-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregular-expression-matching.cpp
More file actions
105 lines (99 loc) · 3.19 KB
/
regular-expression-matching.cpp
File metadata and controls
105 lines (99 loc) · 3.19 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
//https://leetcode.com/problems/regular-expression-matching/
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ROF(i,a,b) for(int i=a;i>b;i--)
#define MEM(x,a) memset(x,a,sizeof(x));
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define ROFEACH(it, l) for (auto it = l.end(); it !=l.begin(); it--)
#define TOUPPER(str) transform(str.begin(), str.end(),str.begin(), ::toupper)
#define TOLOWER(str) transform(str.begin(), str.end(),str.begin(), ::tolower)
typedef long long LL;
template < class T > T MAX( T a, T b ){return ( a > b ? a : b );}
template < class T > T MIN( T a, T b ){return ( a < b ? a : b );}
template<class T> T GCD(T a,T b){if(b == 0)return a;return gcd(b,a%b);}
template<class T> T LCM(T a, T b ){return (a*b)/gcd(a,b);}
string getline(){string inputstr; while (inputstr.length()==0 ){ getline(cin, inputstr);} return inputstr;}//read line from stdin
template <typename T> string to_str(T str){stringstream stream; stream << str; return stream.str();}//Convert int to string
template <typename T>int to_int(T num){int val; stringstream stream; stream<<num; stream>>val; return val;}
vector<string> split(string &s,char delim){vector<string> elems;stringstream ss(s); string item;while(getline(ss,item,delim)){elems.push_back(item);}return elems;}
bool solve(string &s, string &p, int i, int j, vector<vector<int> > &dp){
if(i < s.size() && j >= p.size()){
return false;
}else if(i >= s.size() && j >= p.size()){
return true;
}else if(i >= s.size() && j < p.size()){
int countStar = 0;
int jLeft = p.size() - j - 1;
while(++j < p.size()){
if(p[j] == '*'){
countStar++;
}
j++;
}
return countStar == (jLeft / 2) + 1;
}else{
if(dp[i][j] != -1){
return dp[i][j];
}
if(s[i] != p[j] && p[j] != '.'){
if(p[j + 1] == '*'){
dp[i][j] = solve(s, p, i, j + 2, dp);
}else{
dp[i][j] = false;
}
}else if(s[i] == p[j] || p[j] == '.'){
if(p[j + 1] != '*'){
dp[i][j] = solve(s, p, i + 1, j + 1, dp);
}else{
dp[i][j] = solve(s, p, i + 1, j, dp) || solve(s, p, i, j + 2, dp);
}
}
}
return dp[i][j];
}
bool solve(string &s, string &p){
vector<vector<int> > dp(s.size(), vector<int>(p.size(), -1));
return solve(s, p, 0, 0, dp);
}
int main(){
freopen("1-input", "r", stdin);
freopen("2-output", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
scanf("%d", &t);
while(t--){
string s, p;
cin>>s>>p;
// s="";
// p="c*c*";
bool output = solve(s, p);
bool expected;
cin>>expected;
printf("==============testcase: %d===========\n", t);
cout<<"===>output:"<<output<<" expected:"<<expected<<" result:"<<(output==expected ? "true" : "false")<<endl;
}
return 0;
}
// 9
// ab .*c
// 0
// aaa aaaa
// 0
// aa a*
// 1
// aa a
// 0
// ab .*
// 1
// aab c*a*b
// 1
// mississippi mis*is*p*.
// 0
// abcba c*a.*b
// 0
// abcbab c*a.*b
// 1