-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53_RegularExpressionsMatching.cpp
More file actions
154 lines (118 loc) · 3.31 KB
/
53_RegularExpressionsMatching.cpp
File metadata and controls
154 lines (118 loc) · 3.31 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# include <iostream>
# include <cstdlib>
# include <vector>
# include <cstring>
using namespace std;
void display_vec(vector<int> &vec)
{
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << ' ';
cout << endl;
}
void display_vec(vector<string> &vec)
{
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << ' ';
cout << endl;
}
bool match_eat_repeat(char* str, char* pattern)
{
if (str == NULL || pattern == NULL)
return false;
int i = 0, j = 0;
char repeat = '\0';
while (str[i] != '\0' && pattern[j] != '\0')
{
cout << str[i] << " " << pattern[j] << endl;
// repeat
if (pattern[j + 1] == '*')
{
if(pattern[j] == '.' || str[i] == pattern[j])
{
repeat = str[i];
i++;
}
else
j += 2;
continue;
}
// remove repeat
if (pattern[j] == repeat)
{
j++; continue;
}
repeat = '\0';
// arbitrary
if (pattern[j] == '.' || str[i] == pattern[j])
{
i++; j++;
continue;
}
return false;
}
// reduce star
while (pattern[j] != '\0' && pattern[j + 1] == '*')
j += 2;
// reduce repeat
while (pattern[j] != '\0' && pattern[j] == repeat)
j++;
// match
if (str[i] == '\0' && pattern[j] == '\0')
return true;
return false;
}
bool match_recur(char* str, char* pattern)
{
if (str == NULL || pattern == NULL)
return false;
if (*str == '\0' && *pattern == '\0')
return true;
if (*str != '\0' && *pattern == '\0')
return false;
// second char is '*'
if (*(pattern + 1) == '*')
{
// first char match
if (*str == *pattern || (*str != '\0' && *pattern == '.'))
return match_recur(str + 1, pattern) // stay at current
|| match_recur(str, pattern + 2); // ignore pattern
else // first char doesn't match
return match_recur(str, pattern + 2); // ignore pattern
}
// second char is not '*'
// first char match
if (*str == *pattern || (*str != '\0' && *pattern == '.'))
return match_recur(str + 1, pattern + 1); // move to next
// first char doesn't match
return false;
}
bool match(char* str, char* pattern)
{
if (str == NULL || pattern == NULL)
return false;
int len1 = strlen(str), len2 = strlen(pattern);
vector<vector<bool> > dp(len1 + 1, vector<bool>(len2 + 1, false));
// dynamic programming
dp[len1][len2] = true;
for (int i = len1; i >= 0; i--)
for (int j = len2 - 1; j >= 0; j--)
if (pattern[j + 1] == '*')
if (pattern[j] == str[i] || (i < len1 && pattern[j] == '.'))
dp[i][j] = dp[i][j + 2] || dp[i + 1][j];
else
dp[i][j] = dp[i][j + 2];
else
if (pattern[j] == str[i] || (i < len1 && pattern[j] == '.'))
dp[i][j] = dp[i + 1][j + 1];
return dp[0][0];
}
int main(int argc, char *argv[])
{
char *str = argv[1], *pattern = argv[2];
bool res;
cout << str << endl;
cout << pattern << endl;
res = match(str, pattern);
cout << res << endl;
return 0;
}