-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegular_expression_matching.cpp
More file actions
67 lines (59 loc) · 1.45 KB
/
Regular_expression_matching.cpp
File metadata and controls
67 lines (59 loc) · 1.45 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
#include<bits/stdc++.h>
using namespace std;
bool matching(string str,string pattern){
int n=str.length();
int m=pattern.length();
int table[n+1][m+1];
table[0][0]=1;
int flag=0;
for(int i=1;i<m+1;i++){
if(pattern[i-1]=='*'){
table[0][i]=table[0][i-2];
}else{
table[0][i]=0;
}
}
for(int i=1;i<n+1;i++){
table[i][0]=0;
}
/*
if(flag==1){
for(int i=1;i<m+1;i++){
table[0][i]=0;
}
}else{
for(int i=1;i<m+1;i++){
table[0][i]=1;
}
}
*/
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){
if((str[i-1]==pattern[j-1]) || (pattern[j-1]=='.')){
table[i][j]=table[i-1][j-1];
}else if(pattern[j-1]=='*'){
if(table[i][j-2]==1){
table[i][j]=1;
//cout<<table[i][j]<<endl;
}else{
if(str[i-1]==pattern[j-2] || pattern[j-2]=='.'){
table[i][j]=table[i-1][j];
}
//cout<<table[i][j]<<endl;
}
}else{
table[i][j]=0;
}
}
}
if(table[n][m]==1){
return true;
}
return false;
}
int main(){
string str="aaa";
string pattern="ab*a";
cout<<matching(str,pattern);
return 0;
}