lintcode 通配符匹配 ac代码
来源:互联网 发布:淘宝5.9.5版本下载 编辑:程序博客网 时间:2024/05/16 10:44
今天在lintcode看到一道题,看了一下午的答案都没看懂,深深的挫败感,先记录下来,日后在啃
实现支持'.'和'*'的正则表达式匹配。
'.'匹配任意一个字母。
'*'匹配零个或者多个前面的元素。
匹配应该覆盖整个输入字符串,而不仅仅是一部分。
需要实现的函数是:bool isMatch(const char *s, const char *p)
样例
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
2017/9/1号更新,今天又看了一遍,看懂了一些,这种做法属于动态规划,主线分为当前字母之后的一个字母是*和不是*的两种情况,并且整体风格是递归的向后检测匹配与否,每一个环节只用考虑当前的情况是否匹配,实在是一个好方法
class Solution {public: /** * @param s: A string * @param p: A string includes "." and "*" * @return: A boolean */ bool isMatch(const char *s, const char *p) { // write your code here if(*p == '\0') return *s == '\0'; if(*(p+1) != '*'){ if((*p == '.' && *s != '\0') || *p == *s){//*s如果为结束符即使*p为.当前也不匹配 return isMatch(s+1, p+1); }else{ return false; } }else{ while((*p == '.' && *s != '\0') || *p == *s){ if(isMatch(s, p+2))//每个遇到*号都可以先把它跳过匹配后面的 return true; s++;//match one more * } return isMatch(s, p+2); //这是当前两个字符不相等,寻找*后的字符与s是否相等的情况 } }};
阅读全文
1 0
- lintcode 通配符匹配 ac代码
- lintcode 通配符匹配 ac代码
- lintcode 正则表达式匹配 ac代码
- LintCode-通配符匹配
- LintCode 通配符匹配
- LintCode 通配符匹配
- 通配符匹配-LintCode
- AC自动机通配符匹配
- AC自动机通配符匹配
- LintCode (192)通配符匹配
- [Lintcode]Wildcard Matching 通配符匹配
- 通配符匹配
- 通配符匹配
- 通配符匹配
- 通配符匹配
- AC-BM算法原理与代码实现(模式匹配)
- AC-BM算法原理与代码实现(模式匹配)
- 文件名通配符匹配算法
- 快速排序
- java命令行HPROF Profiler
- 前端国际化
- 关于Linux 学习的整理
- Git学习笔记(4)----基本操作及命令(本地仓库篇)
- lintcode 通配符匹配 ac代码
- UINavigationController的侧滑返回遇到的坑
- shell中$(( ))、$( )与${ }的区别
- 探讨Android6.0及以上系统APP常驻内存(保活)实现-复活篇
- 京东抢购系统
- c# lambda表达式学习(3)
- SpringMVC学习笔记
- 总结四(TCP、UDP)
- java反射