Regular Expression Matching
2015-06-29 03:19
344 查看
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
class Solution { public: bool isMatch(string s, string p) { int m = s.length(); int n = p.length(); bool buf[m+1][n+1]; buf[0][0] = true; for (int i = 1; i <= m; i++) { buf[i][0] = false; } for (int i = 1; i <= n; i++) { if (p[i-1] == '*' && buf[0][i-2]) { buf[0][i] = true; } else { buf[0][i] = false; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s[i-1] == p[j-1] || p[j-1] == '.') { if (buf[i-1][j-1]) { buf[i][j] = true; } else { buf[i][j] = false; } } else if (p[j-1] == '*') { buf[i][j] = buf[i][j-1] || buf[i][j-2] || ((buf[i-1][j-1] || buf[i-1][j]) && (p[j-2] == s[i-1] || p[j-2] == '.')); } else { buf[i][j] = false; } } } return buf[m] ; } };
相关文章推荐
- SQL手工注入的学习总结
- 热门的国人开发开源项目
- HDU 5025
- ECMAScript 6新特性
- 大型DELETE(删除大量数据)的一种解决方案
- Linux内核源码分析方法
- A brief CRC tutorial
- open odoo 调试 debug
- [.net 面向对象程序设计进阶] (2) 正则表达式 (一) 快速入门
- rmmod: chdir(/lib/modules): No such file or directory
- 关于 Java 性能监控您不知道的 5 件事,第 1 部分
- Google interview question: countSort questions
- 在 Java SE 6 中监视和诊断性能问题
- Java SE 6 新特性: JMX 与系统管理
- Java基础学习笔记。
- android 子线程真的不能更新ui吗
- 1013--映射一对一关联关系
- Java SE 6 新特性: Instrumentation 新功能
- Java SE 6 新特性: Java DB 和 JDBC 4.0
- 浅谈“栈和堆”