10. Regular Expression Matching
来源:互联网 发布:对讲机调频软件 编辑:程序博客网 时间:2024/04/26 01:46
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
Solution:
This looks just like a straight forward string matching, isn’t it? Couldn’t we just match the pattern and the input string character by character? The question is, how to match a ‘*’?
A natural way is to use a greedy approach; that is, we attempt to match the previous character as many as we can. Does this work? Let us look at some examples.
s = “abbbc”, p = “ab*c”
Assume we have matched the first ‘a’ on both s and p. When we see “b*” in p, we skip all b’s in s. Since the last ‘c’ matches on both side, they both match.
s = “ac”, p = “ab*c”
After the first ‘a’, we see that there is no b’s to skip for “b*”. We match the last ‘c’ on both side and conclude that they both match.
It seems that being greedy is good. But how about this case?
s = “abbc”, p = “ab*bbc”
When we see “b*” in p, we would have skip all b’s in s. They both should match, but we have no more b’s to match. Therefore, the greedy approach fails in the above case.
One might be tempted to think of a quick workaround. How about counting the number of consecutive b’s in s? If it is smaller or equal to the number of consecutive b’s after “b*” in p, we conclude they both match and continue from there. For the opposite, we conclude there is not a match.
This seem to solve the above problem, but how about this case:
s = “abcbcd”, p = “a.*c.*d”
Here, “.*” in p means repeat ‘.’ 0 or more times. Since ‘.’ can match any character, it is not clear how many times ‘.’ should be repeated. Should the ‘c’ in p matches the first or second ‘c’ in s? Unfortunately, there is no way to tell without using some kind of exhaustive search.
We need some kind of backtracking mechanism such that when a matching fails, we return to the last successful matching state and attempt to match more characters in s with ‘*’. This approach leads naturally to recursion.
The recursion mainly breaks down elegantly to the following two cases:
If the next character of p is NOT ‘*’, then it must match the current character of s. Continue pattern matching with the next character of both s and p.
If the next character of p is ‘*’, then we do a brute force exhaustive matching of 0, 1, or more repeats of current character of p… Until we could not match any more characters.
第一种方法:回溯法
C代码
bool isMatch(char* s, char* p) { if(*p=='\0') return *s=='\0'; // next char is not '*': must match current character if(*(p+1)!='*') { if((*p==*s)||(*p=='.'&&*s!='\0')) return isMatch(s+1,p+1); else return false; }// If the next character of p is ‘*’, then we do a brute force exhaustive matching of 0, 1, // or more repeats of current character of p…// Until we could not match any more characters. else { while((*p==*s)||(*p=='.'&&*s!='\0')) { if(isMatch(s,p+2)) return true; s++; } return isMatch(s,p+2); } return false;}
C++代码
class Solution {public: bool isMatch(string s, string p) { if(p.empty()) return s.empty(); if(p[1]!='*') { if(s[0]==p[0]||(s[0]!='\0'&&p[0]=='.')) return isMatch(s.substr(1),p.substr(1)); else return false; } while(p[0]==s[0]||(p[0]=='.'&&s[0]!='\0')) { if(isMatch(s,p.substr(2))) return true; s=s.substr(1); } return isMatch(s,p.substr(2)); }};
class Solution {public: bool isMatch(string s, string p) { if (p.empty()) return s.empty(); if ('*' == p[1]) // x* matches empty string or at least one character: x* -> xx* // *s is to ensure s is non-empty return (isMatch(s, p.substr(2)) || !s.empty() && (s[0] == p[0] || '.' == p[0]) && isMatch(s.substr(1), p)); else return !s.empty() && (s[0] == p[0] || '.' == p[0]) && isMatch(s.substr(1), p.substr(1)); }};
第二种方法:动态规划
class Solution {public: bool isMatch(string s, string p) { int m=s.size(); int n=p.size(); vector<vector<bool> >Match(m+1,vector<bool>(n+1,false)); Match[0][0]=true; int i,j; for(i=1;i<=m;i++) Match[i][0]=false; for(j=1;j<=n;j++) { if(j==1) Match[0][j]=false; else { if(p[j-1]=='*'&&Match[0][j-2]) Match[0][j]=true; } } for(i=1;i<=m;i++) for(j=1;j<=n;j++) { if(p[j-1]!='*') { if(Match[i-1][j-1]&&(s[i-1]==p[j-1]||p[j-1]=='.')) Match[i][j]=true; else Match[i][j]=false; } else { if(Match[i][j-2]||Match[i-1][j]&&(s[i-1]==p[j-2]||p[j-2]=='.')) Match[i][j]=true; else Match[i][j]=false; } } return Match[m][n]; }};
class Solution {public: bool isMatch(string s, string p) { /** * f[i][j]: if s[0..i-1] matches p[0..j-1] * if p[j - 1] != '*' * f[i][j] = f[i - 1][j - 1] && s[i - 1] == p[j - 1] * if p[j - 1] == '*', denote p[j - 2] with x * f[i][j] is true iff any of the following is true * 1) "x*" repeats 0 time and matches empty: f[i][j - 2] * 2) "x*" repeats >= 1 times and matches "x*x": s[i - 1] == x && f[i - 1][j] * '.' matches any single character */ int m = s.size(), n = p.size(); vector<vector<bool>> f(m + 1, vector<bool>(n + 1, false)); f[0][0] = true; for (int i = 1; i <= m; i++) f[i][0] = false; // p[0.., j - 3, j - 2, j - 1] matches empty iff p[j - 1] is '*' and p[0..j - 3] matches empty for (int j = 1; j <= n; j++) f[0][j] = j > 1 && '*' == p[j - 1] && f[0][j - 2]; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) if (p[j - 1] != '*') f[i][j] = f[i - 1][j - 1] && (s[i - 1] == p[j - 1] || '.' == p[j - 1]); else // p[0] cannot be '*' so no need to check "j > 1" here f[i][j] = f[i][j - 2] || (s[i - 1] == p[j - 2] || '.' == p[j - 2]) && f[i - 1][j]; return f[m][n]; }};
- 10. Regular Expression Matching
- 10.Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- 10. Regular Expression Matching
- Unity 点击运行界面变黑的问题
- bamboo 内部环境变量
- 云信demo如何修改为侧滑删除聊天列表
- 区块链开发(三)编写调试第一个以太坊智能合约
- 解决gitk时中文显示乱码情况
- 10. Regular Expression Matching
- Android欢迎界面(全屏延时自动跳转)
- Spring AOP拦截器调用实现
- 85. Maximal Rectangle
- Codeforces Round #376 (Div. 2) A && B
- Listener监听器(ServletContextListener)
- SASS、SCSS和css的关系
- 配置WAS中的JMS接收MQ消息
- Android软键盘遮挡的四种解决方案