【Leetcode 10】Regular Expression Matching
来源:互联网 发布:网络写手已成高危职业 编辑:程序博客网 时间:2024/06/05 06:13
题意:
给定一个串s,再给定一个正则表达式 p 问 p能不能生成s
样例中较难理解的的是
isMatch("ab", ".*") → true
这可以等价于 ab和 .. 这样就好理解了。
思路:动态规划
先将p做下预处理,变成两个数组,第一个数组是字符,第二个数组标记对应的字符是否可以出现多次。然后dp[i][j] 表示到s串到 i的位置,p串到j 是否能匹配。有3种转移
一: dp[i-1][j-1]为true 且i和j对应的字符相等
二: dp[i-1][j]为true 且i和j对应字符相等,且j那个字符可以出现多次
三: dp[i][j-1]为true 且j对应的字符可以出现多次(这边取出现0次)
上述3种有一种成立则 dp[i][j]=true
代码如下:
class Solution {public: bool isMatch(string s, string p) { int slen=s.length(); int plen=0; int num[p.length()+2]; string a=""; for(int i=0;i<p.length();++i){ if(p[i]=='*') num[plen-1]=-1; else{ num[plen++]=1; a+=p[i]; } } p=a; bool ok[slen+2][plen+2]; memset(ok,0,sizeof(ok)); ok[0][0]=true; for(int j=1;j<=plen;++j){//初始化,注意前面连续为0 的情况,也可以 下面写法写成后推 的形式就可以不用这一步 ,而我用的是前推 if(num[j-1]==-1){ ok[0][j]=true; }else break; } for(int i=1;i<=slen;++i){ for(int j=1;j<=plen;++j){ if(ok[i-1][j-1]){//转移1 if(s[i-1]==p[j-1]||p[j-1]=='.'){ ok[i][j]=true; } } if(ok[i-1][j]){//转移2 if((s[i-1]==p[j-1]||p[j-1]=='.')&&num[j-1]==-1){ ok[i][j]=true; } } if(ok[i][j-1]){//转移3 if(num[j-1]==-1){ ok[i][j]=true; } } } } return ok[slen][plen]; }};
阅读全文
0 0
- Leetcode【10】:Regular Expression Matching
- [leetcode 10] Regular Expression Matching
- LeetCode(10) Regular Expression Matching
- [leetcode 10] Regular Expression Matching
- leetcode.10------------Regular Expression Matching
- [leetcode] 10 Regular Expression Matching
- [leetcode] 10 Regular Expression Matching
- LeetCode 10:《Regular Expression Matching》
- leetcode 10: Regular Expression Matching
- LeetCode 10 - Regular Expression Matching
- Leetcode #10 Regular Expression Matching
- leetcode 10 -- Regular Expression Matching
- LeetCode 10: Regular Expression Matching
- leetcode 10 -- Regular Expression Matching
- leetcode-10Regular Expression Matching
- leetcode-10 Regular Expression Matching
- leetcode 10: Regular Expression Matching
- leetcode 10 Regular Expression Matching
- AsyncTask源码几句话解释
- AS 中 Gradle 配置运行浅析
- MyBatis——批量插入
- Oracle12C--记录类型(三十一)
- Android常见知识汇总
- 【Leetcode 10】Regular Expression Matching
- 初学kotlin记录
- cordova 创建项目
- 细说Wannacry勒索软件所使用的木马技术
- stm32_FSMC注意事项
- 安卓发布代码到 JCenter
- Android Kotlin 开发--初体验Demo
- 最长递增子序列(longest increasing subsequence) 问题详解
- 算法练习