Leetcode Regular Expression Matching
2016-04-16 16:58
393 查看
Leetcode Regular Expression Matching,本题的主要思路是回溯法,算法主要需要处理两种状态:
1. 正常字符时,如’a-z’, ‘.’等, 这类直接匹配。
2. 遇到”x*”形式时,需要使用回溯法,进行逐个测试,直接找到一个可用的子匹配,否非反回false。
相关算法如下,以下算法效率不高,还有很多可优化的地方:
1. 正常字符时,如’a-z’, ‘.’等, 这类直接匹配。
2. 遇到”x*”形式时,需要使用回溯法,进行逐个测试,直接找到一个可用的子匹配,否非反回false。
相关算法如下,以下算法效率不高,还有很多可优化的地方:
#include<iostream> #include<vector> #include<string> #include<map> /** * The method to solve this problem is backtrack. * First match the pure characters include '.' * Second try the "x*" pattern, if find a match return true, if not return false * */ using namespace std; class Solution { public: bool isMatch(string s, string p) { int pos = 0; // remove the duplicated "*" for (int i = 0; i < p.length(); i++) { if (!(p[i] == '*' && (i == 0 || p[i - 1] == '*'))) { p[pos] = p[i]; pos++; } } p = p.substr(0, pos); return match(s, 0, p, 0); } bool match(string& s, int idxs, string& p, int idxp) { // make sure the there are more characters need to match if (idxs >= s.length() && idxp >= p.length()) { return true; } else if (idxp >= p.length()) { return false; } // match the pure character while (idxs < s.length() && idxp < p.length() - 1 && p[idxp + 1] != '*') { if (p[idxp] != s[idxs] && p[idxp] != '.') { return false; } idxp++; idxs++; } // make sure after the preceding match, there state is p encounter the // end or the s encounter the end and the left p pattern is "x*xxxx" if (idxp == p.length() - 1) { if (idxs == s.length() - 1 && (p[idxp] == s[idxs] || p[idxp] == '.')) { return true; } else { return false; } } else if (idxs >= s.length() && p[idxp + 1] != '*') { return false; } // process the "x*" using backtrack int idx = idxs; while (idx < s.length() && (p[idxp] == s[idx] || p[idxp] == '.')) { if (match(s, idx, p, idxp + 2)) { return true; } idx++; } if (idx == s.length() && idxp + 2 == p.length() && p[idxp + 1] == '*') { return true; } return match(s, idx, p, idxp + 2); } }; int main(int argc, char* argv[]) { Solution so; cout<<"result: "<<so.isMatch(string(argv[1]), string(argv[2]))<<endl; return 0; }
测试: ./a.out "abcabcabc" "a*b*c*abcabc" result: 1
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析