-
Notifications
You must be signed in to change notification settings - Fork 0
/
regular_expression_matching.cpp
72 lines (65 loc) · 2 KB
/
regular_expression_matching.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
typedef string::iterator StrIt;
bool isMatch(string s, string p) {
vector<vector<int>> buffer(s.size() + 1, vector<int> (p.size() + 1, -1));
return isMatchCore(s, 0, s.size(), p, 0, p.size(), buffer);
}
bool isMatchCore(string& s, int si, int send, string& p, int pi, int pend,
vector<vector<int>>& buffer)
{
if (buffer[si][pi] == 0)
return false;
if (si == send && pi == pend)
return true;
if (si != send && pi == pend)
{
buffer[si][pi] = 0;
return false;
}
if (si == send && pi != pend)
if (pend - pi >= 2 && p[pi + 1] == '*')
return isMatchCore(s, si, send, p, pi + 2, pend, buffer);
else
{
buffer[si][pi] = 0;
return false;
}
if (s[si] != p[pi] && p[pi] != '.')
if (pend - pi >= 2 && p[pi + 1] == '*')
return isMatchCore(s, si, send, p, pi + 2, pend, buffer);
else
{
buffer[si][pi] = 0;
return false;
}
// (s[si] == p[pi]) || (p[pi] == '.')
if (pend - pi >= 2 && p[pi + 1] == '*')
{
if (isMatchCore(s, si + 1, send, p, pi, pend, buffer) ||
isMatchCore(s, si, send, p, pi + 2, pend, buffer) ||
isMatchCore(s, si + 1, send, p, pi + 2, pend, buffer))
return true;
buffer[si][pi] = 0;
return false;
}
return isMatchCore(s, si + 1, send, p, pi + 1, pend, buffer);
}
};
int main()
{
//string s("aaaaaaaaaaaaab");
//string p("a*a*a*a*a*a*a*a*a*a*c");
string s("ab");
string p(".*c");
Solution solution;
if (solution.isMatch(s, p))
cout << "True" << endl;
else
cout << "False" << endl;
return 0;
}