-
Notifications
You must be signed in to change notification settings - Fork 0
/
Regular-Expression-Matching.cpp
122 lines (85 loc) · 3.05 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
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
/*
* Song Qiang <qiang.song@usc.edu> 2013
*
*/
/*
Implement regular expression matching with support for '.' and '*'.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
*/
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <sstream>
#include <limits>
#include <utility>
using namespace std;
class Solution {
public:
bool isMatch(const char *s, const char *p) {
const int pn = strlen(p);
const int sn = strlen(s);
vector<int> prev;
vector<int> curr;
if (p[0] == '.' || p[0] == s[0])
prev.push_back(0);
if (pn > 2 && p[1] == '*' && (p[2] == '.' || p[2] == s[0]))
prev.push_back(2);
for (int i = 1; i < sn; ++i)
{
cout << s[i - 1] << ":\t";
for_each(prev.begin(), prev.end(), [](int x) {cout << x << "\t";});
cout << endl;
if (prev.empty()) return false;
for (int j = 0; j < prev.size(); ++j)
{
const int prev_id = prev[j];
const int curr_id = prev_id + 1;
if (p[prev_id] == '*' && (p[prev_id - 1] == '.' || s[i] == p[prev_id - 1])) curr.push_back(prev_id);
if (curr_id < pn) {
if (p[curr_id] == '*') {
if (p[prev_id] == '.' || s[i] == p[prev_id]) curr.push_back(curr_id);
if (curr_id + 1 < pn && (p[curr_id + 1] == '.' || s[i] == p[curr_id + 1])) curr.push_back(curr_id + 1);
if (curr_id + 3 < pn && p[curr_id + 2] == '*' && (p[curr_id + 3] == '.' || p[curr_id + 3] == s[i]))
{
curr.push_back(curr_id + 3);
}
} else if (p[curr_id] == '.' || p[curr_id] == s[i])
curr.push_back(curr_id);
}
cout << curr_id << "\t" << p[curr_id] << p[curr_id + 1] << p[curr_id + 2] << endl;
if (curr_id + 2 < pn && p[curr_id + 1] == '*' && (p[curr_id + 2] == '.' || p[curr_id + 2] == s[i]))
{
curr.push_back(curr_id + 2);
}
}
prev.assign(curr.begin(), curr.end());
curr.clear();
}
cout << s[sn - 1] << ":\t";
for_each(prev.begin(), prev.end(), [](int x) {cout << x << "\t";});
cout << endl;
return find(prev.begin(), prev.end(), pn - 1) != prev.end();
}
};
int
main(int argn, char** argv)
{
Solution s;
s.isMatch("aaa", "ab*a*c*a");
}