您的位置:首页 > 其它

Leetcode Regular Expression Matching

2016-04-16 16:58 393 查看
Leetcode Regular Expression Matching,本题的主要思路是回溯法,算法主要需要处理两种状态:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 leetcode