forked from knakul853/spoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NAJPF.cpp
81 lines (69 loc) · 1.42 KB
/
NAJPF.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
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
char txt[1000000];
char pt[1000000];
while(n--){
scanf("%s",txt);
scanf("%s",pt);
int lp = strlen(pt);
int lt = strlen(txt);
int lsp[lp];
lsp[0]=0;
int i=0,j=1;
while(j<lp){
if(pt[i]==pt[j]){
lsp[j]=i+1;
j++;i++;
}
else{
if(i==0)
{
lsp[j]=i;
j++;
}
else{
i=lsp[i-1];
}
}
}
int f=1;
i=0;
j=0;
vector<int> ans;
while(j<lt)
{
if(pt[i]==txt[j])
{
i++;j++;
}
else if(pt[i] != txt[j])
{
if(i==0)
{
j++;
}
else
{
i=lsp[i-1];
}
}
if(i==lp)
{
ans.push_back(j-i+1);
f=0;
}
}
if(f==1){
cout<<"\n"<<"Not Found"<<"\n"<<endl;
}
else{
int l=ans.size();
cout<<l<<endl;
for(int i=0;i<l;i++)
cout<<ans[i]<<" ";
}
}
}