regular expression matching 正则匹配
来源:互联网 发布:淘宝注册页面html模板 编辑:程序博客网 时间:2024/05/16 09:34
/*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") ? falseisMatch("aa","aa") ? trueisMatch("aaa","aa") ? falseisMatch("aa", "a*") ? trueisMatch("aa", ".*") ? trueisMatch("ab", ".*") ? trueisMatch("aab", "c*a*b") ? true*The asterisk indicates zero or more occurrences of the preceding element.For example, ab*c matches "ac", "abc", "abbc", "abbbc", and so on. “*”代表之前的那个字符可以是0个或者任意多个*/#include <stdio.h>#include <string.h>/*采用递归解决: 若p为空,s也为空返回true,否则返回false若p[1]=='*'时,’*‘代表0个前面的字符时,把p的前两个丢掉进行递归;’*‘代表多个前一个字符时,若果s字符长度大于1,则比较完s[0]==p[0]后,把s的第一个字符丢掉然后进行递归。若p[1]!='*'时,且s_len不为零,则把s p的第一个字符都丢掉并递归。*/int isMatch(char* s, char* p){ int s_len=strlen(s);int p_len=strlen(p);if(p_len==0) return s_len==0;if(p_len>1 && p[1]=='*') return isMatch(s,&(p[2])) || (s_len!=0 && (s[0]==p[0]||p[0]=='.') && isMatch(&(s[1]),p));elsereturn s_len!=0 && (s[0]==p[0] || p[0]=='.') && isMatch(&(s[1]),&(p[1]));}/*参考:https://discuss.leetcode.com/topic/17852/9-lines-16ms-c-dp-solutions-with-explanations采用动态规划: This problem has a typical solution using Dynamic Programming. We define the state P[i][j] to be true if s[0..i) matches p[0..j) and false otherwise. Then the state equations are:P[i][j] = P[i-1][j-1],if p[j-1] != '*' && (s[i-1] == p[j-1] || p[j-1] == '.');P[i][j] = P[i][j-2], if p[j-1] == '*' and the pattern repeats for 0 times;P[i][j] = P[i-1][j] && (s[i-1] == p[j-2] || p[j-2] == '.'), if p[j-1] == '*' and the pattern repeats for at least 1 times. */int isMatch_dp(char *s,char *p){int s_len=strlen(s);int p_len=strlen(p);int dp[s_len+1][p_len+1];memset(dp,0,sizeof(dp));dp[0][0]=1;//s p 长度为0时 为真int i,j;for(i=0;i<=s_len;i++) for(j=1;j<=p_len;j++){if(j>1 && p[j-1]=='*')dp[i][j]=dp[i][j-2] || (i>0 && (s[i-1]==p[j-2] || p[j-2]=='.') && dp[i-1][j]);elsedp[i][j]=i>0 && dp[i-1][j-1] && (s[i-1]==p[j-1] || p[j-1]=='.');}return dp[s_len][p_len];}int main(int argc,char **argv){char s[]="aa";char p[]=".*";printf("%d\n",isMatch(s,p));printf("%d\n",isMatch_dp(s,p));return 0;}
阅读全文
0 0
- regular expression matching 正则匹配
- Regular Expression Matching 正则匹配问题
- Regular Expression Matching 正则表达式匹配
- [LeetCode]—Regular Expression Matching 正则匹配
- LeetCode 10 Regular Expression Matching 正则匹配
- 10. Regular Expression Matching&正则表达式匹配
- Regular Expression Matching正则表达式匹配
- [Leetcode P10]Regular Expression Matching 正则匹配
- 正则表达式匹配-leetcode Regular Expression Matching
- 正则表达式匹配(Regular Expression Matching)
- Regular Expression Matching:仿正则匹配字符串
- LeetCode 面试题 Regular expression matching(正则匹配)
- 简单的正则表达式匹配 Regular Expression Matching
- 多正则表达式匹配(Multiple Regular Expression Matching)
- 判断正则表达式是否匹配 Regular Expression Matching @LeetCode
- LeetCode OJ 之 Regular Expression Matching (正则表达式匹配)
- Regular Expression Matching 正则表达式匹配和递归
- LeetCode 10 Regular Expression Matching (正则表达式匹配)
- 数据挖掘流程
- springboot中配置restTemplate及简单使用
- Python入门系列——第1篇
- 计算机网络基础学习
- 谓词的使用(NSPredicate)
- regular expression matching 正则匹配
- JVM菜鸟进阶高手之路二(JVM的重要性,Xmn是跟请求量有关。)
- 代理模式
- hdu1814 Peaceful Commission(2-sat)
- Emag eht htiw Em Pleh
- IOS开发中,蓝牙开发中数据,字符串转化成uint8数组
- 优酷菜单栏
- LEADTOOLS v19试用版安装指南图文详解
- 利用GitHub进行敏捷开发管理