标签:
Implement regular expression matching with support for ‘.‘
and ‘*‘
.
‘.‘ 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") → 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
0 | ..... | i-1 | i | ||
0 | ..... | j-1 | j * |
1 class Solution { 2 public: 3 bool isMatch(string s, string p) { 4 //constexpr int len1 = static_cast<const int>(s.length()), len2 = p.length(); 5 //bool res[len1][len2] = { 0 }; 这儿自己原本是想直接用数组,但是数组下标是要求常量表达式的,着急啊,现在也没解决 6 int len1 = s.length() + 1, len2 = p.length() + 1; 7 vector<vector<bool>> res(len1, vector<bool>(len2, false)); 8 res[0][0] = true; 9 for (int i = 1; i < len2;i++)//没有这3句, "aab", "c*a*b" 会不通过 10 if (p[i - 1] == ‘*‘) 11 res[0][i] = res[0][i - 2]; 12 13 for (int j = 0; j < len2-1; j++) 14 { 15 16 if (p[j] == ‘*‘) 17 { 18 19 if (j>0&&p[j - 1] != ‘.‘) 20 { 21 for (int i = 0; i < len1-1; ++i) 22 if (res[i + 1][j - 1] || res[i + 1][j] ||i>0&& s[i - 1] == s[i] && s[i - 1] == p[j - 1]&& (res[i][j + 1]||res[i][j]))//这个地方一定要注意在具体的条件上加上限制就好了,千万别去将前面的for循环由i=0改为i=1 23 res[i + 1][j + 1] = true; 24 } 25 else 26 { 27 int i = 0; 28 // for (; i < len1;) 29 //if (!res[i+1][j - 1] && !res[i][j]) 30 // ++i; 这个地方竟然写了个死循环 31 while (j>0&&i < len1-1&&!res[i + 1][j - 1] && !res[i+1][j]) 32 ++i; 33 for (; i < len1-1; ++i) 34 res[i + 1][j + 1] = true; 35 36 } 37 } 38 else 39 { 40 41 for (int i = 0; i < len1-1;++i) 42 if ((s[i] == p[j] || p[j] == ‘.‘) && res[i][j]) 43 res[i + 1][j + 1] = true; 44 } 45 } 46 return res[len1 - 1][len2 - 1]; 47 } 48 };
regular expression matching DP
标签:
原文地址:http://www.cnblogs.com/chess/p/4713506.html