第10题 正则表达式匹配(动态规划)
来源:互联网 发布:淘宝卖家能删除差评 编辑:程序博客网 时间:2024/06/03 22:06
题目描述:
'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char *s, const char *p)Some examples:isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → true
使用字符串p来表示字符串s,看是否匹配。比如“c*a*b“可以匹配”aab“,此时第一个*表示有0个c,第二个*表示有1个a。
思路:
1, If p.charAt(j) == s.charAt(i) : dp[i][j] = dp[i-1][j-1];2, If p.charAt(j) == '.' : dp[i][j] = dp[i-1][j-1];3, If p.charAt(j) == '*': here are two sub conditions: 1 if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2] //in this case, a* only counts as empty 2 if p.charAt(i-1) == s.charAt(i) or p.charAt(i-1) == '.': dp[i][j] = dp[i-1][j] //in this case, a* counts as multiple a or dp[i][j] = dp[i][j-1] // in this case, a* counts as single a or dp[i][j] = dp[i][j-2] // in this case, a* counts as empty
比如:s="aaab" , p="c*a*b"
c*a*ba10101a00000a00000b0000
代码:
package T010;public class RegularExpressionMatching { public static void main(String[] args) { System.out.println(isMatch("aab","c*a*b")); } public static boolean isMatch(String s, String p) { if (s == null || p == null) { return false; } boolean[][] dp = new boolean[s.length()+1][p.length()+1]; dp[0][0] = true; for (int i = 0; i < p.length(); i++) { if (p.charAt(i) == '*' && dp[0][i-1]) { dp[0][i+1] = true; } } printArray(dp); for (int i = 0 ; i < s.length(); i++) { for (int j = 0; j < p.length(); j++) { if (p.charAt(j) == '.' || p.charAt(j) == s.charAt(i)) { dp[i+1][j+1] = dp[i][j]; } if (p.charAt(j) == '*') { if (p.charAt(j-1) != s.charAt(i) && p.charAt(j-1) != '.') { dp[i+1][j+1] = dp[i+1][j-1]; } else { dp[i+1][j+1] = (dp[i+1][j] || dp[i][j+1] || dp[i+1][j-1]); } } } } // printArray(dp); return dp[s.length()][p.length()]; } /* * isMatch("aa","a") → false * isMatch("aa","aa") → true * isMatch("aaa","aa") → false * isMatch("aa", "a*") → true * isMatch("aa", ".*") → true * isMatch("ab", ".*") → true * isMatch("aab", "c*a*b") → true */ public static void printArray(boolean [] arr){ System.out.print("printArray:"); for(int i=0;i<arr.length;i++){ System.out.print(arr[i]+" "); } System.out.println(""); } public static void printArray(boolean[][] V){ System.out.println("printArray:"); int rows = V.length; int cols = V[0].length; for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ System.out.print(V[i][j]+" "); } System.out.println(""); } System.out.println(""); }}
0 0
- 第10题 正则表达式匹配(动态规划)
- 【动态规划】正则表达式匹配
- 动态规划——正则表达式匹配
- String、动态规划——正则表达式匹配
- 犀牛 ——第10章 正则表达式的模式匹配 10.1 正则表达式的定义
- LeetCode 10 正则表达式匹配
- 10、正则表达式匹配练习
- 预匹配(正则表达式)
- 匹配邮箱(正则表达式)
- 正则表达式匹配(字符串)
- 动态规划--(加法表达式)
- JavaScript权威指南_81_第10章_正则表达式的模式匹配_10.1.0-正则表达式的定义-概述
- JavaScript权威指南_84_第10章_正则表达式的模式匹配_10.1.3-正则表达式的定义-重复
- nyoj 15括号匹配 (动态规划)
- 括号匹配问题(动态规划)
- nyoj-括号匹配(二)---动态规划
- NYOJ-括号匹配(二)-动态规划
- 动态规划 括号匹配
- java缓存类的编码
- 第5题 查找字符串中的最长回文字符串---Manacher算法
- 第17章 程序管理与SELinux初探
- 第6题 ZigZag转换
- 动态规划
- 第10题 正则表达式匹配(动态规划)
- android videoview
- 第18章 认识系统服务(daemons)
- 微软“咸鱼翻身”:股价沉沦17年后创历史新高
- 第22章 软件安装:源码与Tarball
- EventBus3.0——Stick和Priority
- 第11题 容器装水问题
- 第八周 自由落体问题
- EXCEL 2016年1月2日 转成 2016-01-02