Regular Expression Matching
2016-07-21 16:46
441 查看
Description:
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”) → 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
解题思路:
题意为实现正则表达式的匹配。其中支持.和*
分成三种情况。设当前字符串和模式的下标分别为i,j
1、若当前模式匹配完毕,即p[j]==’\0’,若字符串结束,则返回true,否则返回false
2、若模式的下一个字符不是,即p[j+1]!=’‘。这里分情况讨论。
(1) 若s[i]==p[j],递归验证i+1, j+1
(2) 若p[i]==’.’且s[i]!=’\0’,递归验证i+1, j+1
(3) 否则,返回false
3、若模式的下一个字符是,即p[j+1]==’‘,则不断通过递归回溯i+k,j+2(k从0增长至len(s)-i,j+2意味着越过当前字符和*)。
代码:
测试:
参考链接:
http://www.2cto.com/kf/201404/290845.html
http://www.2cto.com/kf/201506/410752.html
http://www.acmerblog.com/leetcode-solution-regular-expression-matching-6221.html
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”) → 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
解题思路:
题意为实现正则表达式的匹配。其中支持.和*
分成三种情况。设当前字符串和模式的下标分别为i,j
1、若当前模式匹配完毕,即p[j]==’\0’,若字符串结束,则返回true,否则返回false
2、若模式的下一个字符不是,即p[j+1]!=’‘。这里分情况讨论。
(1) 若s[i]==p[j],递归验证i+1, j+1
(2) 若p[i]==’.’且s[i]!=’\0’,递归验证i+1, j+1
(3) 否则,返回false
3、若模式的下一个字符是,即p[j+1]==’‘,则不断通过递归回溯i+k,j+2(k从0增长至len(s)-i,j+2意味着越过当前字符和*)。
代码:
#include <iostream> using namespace std; bool isMatch(const char *s, const char *p) { if (*p == '\0') return *s == '\0'; if (*(p+1) != '*') { if ((*s == *p) || (*p == '.' && *s != '/0')) return isMatch(s+1, p+1); else return false; } else { while((*s == *p) || (*p == '.' && *s != '/0')) { if(isMatch(s, p+2)) return true; s++; } return isMatch(s, p+2); } } int main() { char *s1 = "abbbc", *p1 = "ab*c"; char *s2 = "ac", *p2 = "ab*c"; char *s3 = "abbc", *p3 = "ab*bbc"; char *s4 = "abc", *p4 = "abbc"; cout<<boolalpha<<isMatch(s1, p1)<<endl; cout<<boolalpha<<isMatch(s2, p2)<<endl; cout<<boolalpha<<isMatch(s3, p3)<<endl; cout<<boolalpha<<isMatch(s4, p4)<<endl; return 0; }
测试:
参考链接:
http://www.2cto.com/kf/201404/290845.html
http://www.2cto.com/kf/201506/410752.html
http://www.acmerblog.com/leetcode-solution-regular-expression-matching-6221.html
相关文章推荐
- MongoDB 安装以及系统服务配置方法
- [并发编程]并发编程第一篇:利用并发编程,实现查找大量数据中的素数
- Linux 中查看网口流量的利器 -- sar
- Linux系统修改编码(转)
- [leetcode] 331. Verify Preorder Serialization of a Binary Tree
- C语言开发环境搭建
- jtds 链接服务器 tds protocol error invalid table tab_name_token
- 【基于QMediaPlayer的简易视频播放器】— 3、结合QSlider实现播放进度控制和音量控制
- EntityFramework 入门 CRUD(新增、查询、修改、删除)
- 完美实现跨域Iframe高度自适应【Iframe跨域高度自适应解决方案】
- 常见算法与数据结构整理
- sap中如何配置根据销售订单进行Invoice
- 【杭电oj】1027 - Ignatius and the Princess II(STL - 全排列)
- android TextView中文字通过SpannableString设置属性
- poj 2409 【polya计数】
- Linux Shell中的特殊符号和含义简明总结(包含了绝大部份)
- Volatility 为Centos6.6系统做profile方法
- Unity实现绘制线断二-----用GL画矩形线框
- Android studio基本使用(1)
- FLASH禁止覆盖绝对定位 wmode="opaque"