leetcode算法每天一题010: 正则表达式,判断pattern和string是否匹配(动态规划)
创始人
2024-04-03 14:50:20
0

题目描述

  • ‘.’ 匹配任意单个字符
  • ‘*’ 匹配零个或多个前面的那一个元素
PATTERNTRUEFALSE
a.baab,abb,acba, ab,b
a*bb,ab,aab,aaaba,abb,acb
c*a.baab,caab,cccccacb,ccabbbaab,cabbb

  • dp[i] [j] 的含义是当字符串 s 的长度为 i,模式串 p 的长度为 j 时,两者是否匹配

  • 同一行往前走两个代表的是 a* 等于 空 ,此时, dp[i] [j] = dp[i] [j – 2]

  • 如果当前的字符和之前的字符一样,也是可以match的,只需要看看这match的部分之前是否是match的即可,也就是去看
    dp[i-1][j-2]
    又因为当前为
    的情况 dp[i] [j] = dp[i] [j – 2],可以dp[i-1][j]

在这里插入图片描述

 https://alchemist-al.com/algorithms/regular-expression

class Solution {
public:bool isMatch(string s, string p) {int dp[s.length()+1][p.length()+1];int m, n ;m= s.length();n =  p.length(); //初始化memset(dp,0,sizeof(dp));//https://blog.csdn.net/weixin_44807751/article/details/103793473dp[0][0]= true;for(int j=1;j//对第一行的所有列进行初始化if (p[j-1] == '*')dp[0][j] = dp[0][j-2];elsedp[0][j] = false;}// 继续填写for(int i=1;ifor(int j=1;jif (p[j-1]=='.' || p[j-1]==s[i-1]){dp[i][j] =dp[i-1][j-1];}else if(p[j-1]=='*' && j>=2){if(p[j-2] == s[i-1] || p[j-1-1]=='.'){dp[i][j] = dp[i-1][j] || dp[i][j-2];}else{dp[i][j] =  dp[i][j-2];}}else{dp[i][j] = false;}}}return dp[m][n];}
};

在这里插入图片描述

参考与更多

  • 暴力匹配做法 执行用时:0 ms, 在所有 C 提交中击败了100.00%的用户
bool isMatch(char * s, char * p)
{int slen = strlen(s);int plen = strlen(p);char c;int i,j;int ret;if(slen == 0 && plen != 0 && *(p + 1) != '*'){//如果s串已空而p串不可能空,不匹配return 0;}if(plen > 0 && *(p + 0) == '.'){//如果p串第一个字符是'.'if(plen > 1 && *(p + 1) == '*'){//如果p串第二个字符是'*'//跳过带'*'的字串,*(p + j)第一个不跟'*'的字符for(j = 2; j + 1 < plen && *(p + j + 1) == '*'; j += 2);if(plen == j){//p串已经到了末尾return 1;}else{for(i = 0; i < slen; i ++){//考虑到'.*'的特殊性只能挨个将字串与之尝试匹配if(*(s + i) == *(p + j) || *(p + j) == '.'){if(isMatch(s + i, p + j)){return 1;}}}return 0;}}else//'.'匹配到任意字符{return isMatch(s + 1,  p + 1);}}else if(plen > 0){//如果p串第一个字符是非'.'的字符if(plen > 1 && *(p + 1) == '*'){//如果p串第二个字符是'*'//跳过与p串首字符相等且带'*'的字串,*(p + j)第一个不跟'*'或不与p串首字符相等的字符for(j = 2; j + 1 < plen && *(p + j) == *(p + 0) && *(p + j + 1) == '*'; j += 2);//if(plen == j){//p串已经到了末尾for(i = 0; i < slen && *(s + i) == *(p + 0); i ++);if(i == slen) {return 1;}else{return 0;}}else{//考虑到带'*'的特殊性只能挨个将字串与之尝试匹配for(i = 0; i < slen && *(s + i) == *(p + 0); i ++){if(isMatch(s + i, p + j)){return 1;}}return isMatch(s + i, p + j);}}else if(*s == *p){//两个字串首字符相等匹配到任意字符return isMatch(s + 1,  p + 1);}else return 0;}//p串空后根据s串的剩余长度判断结果if(slen > 0){return 0;}else{return 1;}
}

  • 逆推法
  • 状态归纳+边界条件
public static int uniquePaths(int m, int n) {if (m <= 0 || n <= 0) {return 0;}int[][] dp = new int[m][n]; // // 初始化for(int i = 0; i < m; i++){dp[i][0] = 1;}for(int i = 0; i < n; i++){dp[0][i] = 1;}// 推导出 dp[m-1][n-1]for (int i = 1; i < m; i++) {for (int j = 1; j < n; j++) {dp[i][j] = dp[i-1][j] + dp[i][j-1];}}return dp[m-1][n-1];
}

视频图解 动态规划 正则表达式

  • 用两个指针顺序读取是无法实现的,因为不知道*需要匹配几个(需依次比较,当某个不匹配时即为匹配的个数)。

添加链接描述

添加链接描述

class Solution {
public:bool isMatch(string s, string p) {if (p.empty()) return s.empty();auto first_match = !s.empty() && (s[0] == p[0] || p[0] == '.');if (p.length() >= 2 && p[1] == '*') {return isMatch(s, p.substr(2)) || (first_match && isMatch(s.substr(1), p));} else {return first_match && isMatch(s.substr(1), p.substr(1));}}

https://blog.csdn.net/ResumeProject/article/details/127430933

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...