Wildcard Matching
来源:互联网 发布:网络竞价排名 编辑:程序博客网 时间:2024/06/05 03:02
题目:Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).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", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false
思路:
贪心算法。
首先使用指针会方便很多,如果p=s或者p=?,那么后移,如果p=*,这个时候记住p和s的位置,continue,机关下次可能依然有
p=s或者p=?,p=*等情况,但只有接下来没有这些特殊情况后,p重新回到最近的start+1的地方,因为它能够代替许多长度的字符。
当然,最后还需要考虑是否存在s结束,p还没有结束的情况。如果有,p一直后移。
代码:
class Solution1 {public: bool isMatch(string s, string p) { char *ss=&s[0];char *pp=&p[0]; char *start=NULL,*rs=NULL; while(*ss!='\0'){ if(*ss==*pp||*pp=='?'){ ss++;pp++; continue; } if(*pp=='*'){ start=pp; pp++;//这个时候从下一位开始匹配,因为之前的p可以匹配0位的,1位的.... rs=ss; continue; } //上面两种情况都不符合,那就让之前的*来匹配 if(start!=nullptr){ pp=start+1; ss=rs;//从第0位开始匹配 rs++; continue; } return false; } while(*pp=='*'){ pp++; } return *pp=='\0'; }};class Solution2 {//https://leetcode.com/problems/wildcard-matching///动态规划public: bool isMatch(string s, string p) { int m = s.size(); int n = p.size(); vector<vector<bool> >dp(m+1,vector<bool>(n+1,false)); dp[0][0]=true; // dp[i][j] 代表s[0..i-1]与p[0...j-1]是否匹配 for(int i=1;i<=n;i++){ dp[0][i] = p[i-1]=='*' && dp[0][i-1]; }//这个时候i=0,还没有进入s,只有 p[j-1]=='*' 且dp[0][j-1] 才行 for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ if(p[j-1]=='*'){ dp[i][j] = dp[i-1][j]||dp[i][j-1] ; //按照道理,应该是 //dp[i][j] = dp[i][j-1]||dp[i-1][j-1]||dp[i-2][j-1]||dp[i-3][j-1] ||....||dp[0][j-1] // 匹配0个 匹配1个 匹配2个 匹配3个 //dp[i-1][j]= dp[i-1][j-1]||dp[i-2][j-1]||dp[i-3][j-1] ||....||dp[0][j-1] // 匹配0个 匹配1个 匹配2个 //所以dp[i][j] = dp[i-1][j]||dp[i][j-1] //只是每次迭代其实dp[i-1][j-1]已经把后面那部分算过了。 } else if(p[j-1]=='?'||s[i-1]==p[j-1]){ dp[i][j]=dp[i-1][j-1]; } } } return dp[m][n]; }};
0 0
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Eclipse代码托管到GitHub
- fs4412开发板学习笔记(七)
- android自定义view实现字体变色
- 143 Reorder List [Leetcode]
- 顺便贴一下无聊的Java作业20151027
- Wildcard Matching
- 进制转换
- Codeforces Round #327 (Div. 1) E. Birthday【AC自动机+网络流】
- codeforces#292B_Drazil and His Happy Friends-暴力水题
- Codeforces 590B Chip 'n Dale Rescue Rangers
- Cookie深度解析
- fs4412开发板学习笔记(八)
- NSOperation的介绍
- 文件读写