欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode Regular Expression Matching

发布时间:2025/6/15 编程问答 31 豆豆
生活随笔 收集整理的这篇文章主要介绍了 LeetCode Regular Expression Matching 小编觉得挺不错的,现在分享给大家,帮大家做个参考.


‘.’ 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


多阶段决策过程的最优化问题

子问题的重叠性

p[i][j]  表示字串 s[i...len(s)], p[j...len(p)] 是否可以匹配。

递归地定义最优值

如果P[j+1]!='*',S[i] == P[j]=>匹配下一位(i+1, j+1),S[i]!=P[j]=>匹配失败;

如果P[j+1]=='*',S[i]==P[j]=>匹配下一位(i+1, j+2)或者(i, j+2),S[i]!=P[j]=>匹配下一位(i,j+2)。

匹配成功的条件为S[i]=='\0' && P[j]=='\0'。

dp[i][j] = c1. p[j+1] != *.   if s[i] == p[j]  dp[i][j] = dp[i+1][j+1]       else dp[i][j] = false               

c2 p[j+1] == '*'         if( s[i] ==  p[j] || p[j] == '.' && (*s) != 0) 

p[j] == .  因为他可以匹配任何字符,所以和相等关系有基本一样的方式。


(1)p[j+1]不是'*'。情况比较简单,只要判断如果当前s的i和p的j上的字符一样(如果有p在j上的字符是'.',也是相同),并且res[i][j]==true,则res[i+1][j+1]也为true,res[i+1][j+1]=false; 
(2)p[j+1]是'*',但是p[j]!='.'。那么只要以下条件有一个满足即可对res[i+1][j+1]赋值为true: 
    1)res[i+1][j]为真('*'只取前面字符一次); 
    2)res[i+1][j-1]为真('*'前面字符一次都不取,也就是忽略这两个字符); 
    3)res[i][j+1] && s[i]==s[i-1] && s[i-1]==p[j-1](这种情况是相当于i从0到s.length()扫过来,如果p[j+1]对应的字符是‘*’那就意味着接下来的串就可以依次匹配下来,如果下面的字符一直重复,并且就是‘*’前面的那个字符)。 
(3)p[j+1]是'*',并且p[j]=='.'。因为".*"可以匹配任意字符串,所以在前面的res[i+1][j-1]或者res[i+1][j]中只要有i+1是true,那么剩下的res[i+1][j+1],res[i+2][j+1],...,res[s.length()][j+1]就都是true了。 
这道题有个很重要的点,就是实现的时候外层循环应该是p,然后待匹配串s内层循环扫过来

res[][] 保持中间计算值

/** regular.cpp** Created on: 2014年12月26日* Author: judyge*/ #include <iostream> #include <string> #include<stdio.h> #include<stdlib.h> #include<time.h> #include<windows.h> using namespace std;bool isMatch(string s, string p) {if(s.length()==0 && p.length()==0)return true;if(p.length()==0)return false;bool res[s.length()+1][p.length()+1];res[0][0] = true;for(int j=0;j<p.length();j++){if(p[j]=='*'){if(j>0 && res[0][j-1]) res[0][j+1]=true;if(j<1) continue;if(p[j-1]!='.'){for(int i=0;i<s.length();i++){if(res[i+1][j] || j>0&&res[i+1][j-1]|| i>0 && j>0 && res[i][j+1]&&s[i]==s[i-1]&&s[i-1]==p[j-1])res[i+1][j+1] = true;}}else{int i=0;while(j>0 && i<s.length() && !res[i+1][j-1] && !res[i+1][j])i++;for(;i<s.length();i++){res[i+1][j+1] = true;}}}else{for(int i=0;i<s.length();i++){if(s[i]==p[j] || p[j]=='.')res[i+1][j+1] = res[i][j];}}}return res[s.length()][p.length()]; }int main(){char *s="aab";char *p="c*a*b";char *m="aaa";char *n="aa";clock_t start,finish;double time;start=clock();cout<<isMatch(s,p)<<"\n";cout<<isMatch(m,n)<<"\n";finish=clock();time=(double)((finish-start)/CLOCKS_PER_SEC);printf("start:%ld\t\tfinish:%ld\tfinish-start:%ld\truntime:%f\n",start,finish,finish-start,time);return 0;}运行结果


1 0 start:1 finish:1 finish-start:0 runtime:0.000000

总结

以上是生活随笔为你收集整理的LeetCode Regular Expression Matching的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。