Regular Expression Matching
2015-10-04 10:53
369 查看
Implement regular expression matching with support for
<pre name="code" class="cpp"> bool isMatch(const char *s, const char *p) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (*p == '\0') return *s == '\0';
if (*(p+1) == '*') // 模式串的下一个字符是'*'
{
while(*p == *s || (*p == '.' && *s != '\0'))
{
//字符串与模式串匹配0/1/2...个字符的情况
if (isMatch(s++, p+2))
return true;
}
// 字符串与模式串不能匹配
return isMatch(s, p+2);
}
else
{
if (*p == *s || (*p == '.' && *s != '\0'))
return isMatch(s+1, p+1);
return false;
}
}
'.'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
<pre name="code" class="cpp"> bool isMatch(const char *s, const char *p) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (*p == '\0') return *s == '\0';
if (*(p+1) == '*') // 模式串的下一个字符是'*'
{
while(*p == *s || (*p == '.' && *s != '\0'))
{
//字符串与模式串匹配0/1/2...个字符的情况
if (isMatch(s++, p+2))
return true;
}
// 字符串与模式串不能匹配
return isMatch(s, p+2);
}
else
{
if (*p == *s || (*p == '.' && *s != '\0'))
return isMatch(s+1, p+1);
return false;
}
}
相关文章推荐
- 证券基础--股票发行上市
- for循环计算某个数的阶乘、阶乘和及其倒数的阶乘和
- 使用脚本获得 消耗Linux操作系统内存的前10个用户
- 白盒测试概述及其方法简介
- Action处理请求参数
- Little Kings - SGU 223(状态压缩)
- 数据挖掘之分类
- C Primer Plus第五版记录
- LoadRunner测试下载功能点脚本(方法二)
- 测验纠错
- hdu 5495 LCS (bestcoder #58 1002)
- 数据库锁
- Codeforces Round #323 (Div. 2) C. GCD Table
- Browser 相关内容
- [置顶] [about myself] 关于我自己
- .NET反编译之Reflector基础示例
- [LintCode] Word Break
- 分析建模-如何识别分析类?
- [置顶] iOS开发之导出ipa文件以及查看内容
- 动态链接库与静态链接库的区别