forked from knakul853/spoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NHAY.cpp
76 lines (62 loc) · 1.23 KB
/
NHAY.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
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,f;
char pt[1000000];
char txt[1000000];
while(scanf("%d",&n)!= EOF){
scanf("%s %s",pt,txt);
//cout<<pt[5]<<" "<<txt[5];
int lp = strlen(pt);
int lt = strlen(txt);
int lps[lp];
f=0;
int i=0;
int j=1;
lps[0]=0;
while(j<lp)
{
if(pt[j]==pt[i])
{
lps[j]=i+1;
i++;j++;
}
else{
if(i>0)
{
i=lps[i-1];
}
else{
lps[j]=i;
j++;
}
}
}
f=0;
i=0;j=0;
while(j<lt){
if(pt[i]==txt[j])
{
i++;j++;
}
else if(pt[i] !=txt[j])
{
if(i==0)
{
j++;
}
else{
i=lps[i-1];
}
}
if(i==lp){
cout<<j-i<<endl;
i=lps[i-1];
f=1;
}
}
if(f==0){
cout<<"\n"<<endl;
}
}
}