-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNHAY_KMP.cpp
More file actions
115 lines (91 loc) · 1.46 KB
/
Copy pathNHAY_KMP.cpp
File metadata and controls
115 lines (91 loc) · 1.46 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
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
int main(){
while(!feof(stdin)){
long long int n;
cin>>n;
cin.ignore();
string txt,pt;
//getline(cin,txt);
getline(cin,pt);
getline(cin,txt);
long long int l=pt.length();
vector<int> pt1(l);
long long i=1,j=0;
int flag=1;
while(flag){
l1: while(i<l && (pt.substr(i,1)!=pt.substr(j,1))){
i++;
}
if(i==l){
flag=0;
break;
}
l: while(pt.substr(i,1)==pt.substr(j,1)){
pt1[i]=j+1;
i++;j++;
if(i==l){
flag=0;
break;
}
}
j=pt1[j-1];
if(j>0){
goto l;
}
else{
goto l1;
}
}
/*cout<<pt<<endl;
for(int x=0;x<l;x++){
cout<<pt1[x];
}*/
// Initial text processing done
i=0;j=0;
flag=1;
int first_time=0; long long int rcd=-1; long long int count=0;
while(flag){
l2: while(txt.substr(j,1)!=pt.substr(i,1)){
j++;
if(j==txt.length()){
flag=0;
break;
}
}
if(first_time==0){
rcd=j;
first_time=1;}
while(txt.substr(j,1)==pt.substr(i,1)){
i++;j++;
if(i==l){
cout<<rcd<<endl;
count++;
i=0;
first_time=0;
j=rcd+1;
rcd=-1;
goto l2;
}
if(j==txt.length()){
flag=0;
break;
}
}
i=pt1[i-1];
if(i==0){
rcd=-1;
first_time=0;
}
}
//cout<<rcd<<endl;
if(count==0){
cout<<endl;
}
cout<<endl;
}
return 0;
}