-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20170724 - Regular Expression Matching
81 lines (77 loc) · 1.79 KB
/
20170724 - Regular Expression Matching
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
73
74
75
76
77
78
79
80
81
std::string SimpleString(std::string s) {
for (int i = 0; i + 3 < s.length(); i++) {
if (s[i + 1] == s[i + 3] && s[i + 1] == '*' && s[i] == s[i + 2])
{
s.erase(i, 2);
i--;
}
}
return s;
}
bool CompareString(std::string s, std::string p, int i, int j) {
int slen = s.length();
int plen = p.length();
bool comp;
if (i < slen && j < plen)
{
comp = false;
if (s[i] == '.' || p[j] == '.'|| s[i] == p[j])
{
comp = CompareString(s, p, i + 1, j + 1);
if (comp) return comp;
}
if (p[j] == '*')
{
if (p[j - 1] == s[i] || p[j - 1] == '.') { comp = CompareString(s, p, i , j - 1 ); if (comp) return comp;}
comp = CompareString(s, p, i, j + 1);
if (comp) return comp;
}
else if (j + 1 < plen - 1 && p[j + 1] == '*') { comp = CompareString(s, p, i, j + 2); if (comp) return comp; }
if (s[i] == '*')
{
if (s[i - 1] == p[j] || s[i - 1] == '.') { comp = CompareString(s, p, i - 1, j);if (comp) return comp;}
comp = CompareString(s, p, i + 1, j);
if (comp) return comp;
}
else if (i + 1 < slen - 1 && s[i + 1] == '*') { comp = CompareString(s, p, i + 2, j); if (comp) return comp;}
}
else {
if (i == slen && j == plen) return (s[i - 1] == p[j - 1] || s[i - 1] == '.' || p[j - 1] == '.');
else if (i == slen && j != plen)
{
if (p[j] == '*' && j + 1 == plen)
{
return true;
}
else if (j + 1 != plen)
{
plen = plen - 1;
while (j <= plen)
{
if (p[plen] != '*') return false;
plen = plen - 2;
}
return true;
}
}
else if (j == plen && i != slen)
{
if (s[i] == '*' && i + 1 == slen)
{
return true;
}
else if (i + 1 != slen)
{
slen = slen - 1;
while (i <= slen)
{
if (s[slen] != '*') return false;
slen = slen - 2;
}
return true;
}
}
return false;
}
return comp;
}