-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPOJ_Patter_Find.cpp
More file actions
95 lines (77 loc) · 1.72 KB
/
Copy pathSPOJ_Patter_Find.cpp
File metadata and controls
95 lines (77 loc) · 1.72 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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const ll p = 31;
const ll mod = 1e9+7;
const ll mxN = 1e6+10;
vector<ll> pp(mxN);
vector<ll> invpp(mxN);
ll modularInverse(ll a){
ll exp = mod-2;
ll result = 1;
while(exp){
if(exp%2) result = (result*a)%mod;
a = (a*a)%mod;
exp /= 2;
}
return result;
}
void solve(){
string a,b;
cin>>a>>b;
ll n1 = a.size();
ll n2 = b.size();
if(n2>n1){
cout<<"Not Found"<<endl;
return;
}
vector<ll> h1(n1);
ll h2 = 0;
h1[0] = (ll)(a[0]-'a'+1);
for(ll i=1; i<n1; i++){
h1[i] = (h1[i-1] + ((ll)(a[i]-'a'+1)*pp[i]))%mod;
}
for(ll i=0; i<n2; i++){
h2 += ((ll)(b[i]-'a'+1) * pp[i]);
h2 %= mod;
}
vector<ll> results;
for(ll i = n2 -1; i<n1; i++){
ll l = i - n2;
ll hSub = h1[i];
if(l >= 0){
hSub = (hSub + mod - h1[l]) % mod;
hSub *= invpp[l+1];
hSub %= mod;
}
if(hSub == h2) results.push_back(l+2);
}
ll n = results.size();
if(n==0){
cout<<"Not Found"<<endl;
return;
}
cout<<n<<endl;
for(ll i=0; i<n; i++) cout<<results[i]<<" ";
cout<<endl;
}
signed main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
optimize();
pp[0] = 1;
invpp[0] = modularInverse(pp[0]);
for(ll i=1; i<mxN; i++){
pp[i] = (pp[i-1]*p)%mod;
invpp[i] = modularInverse(pp[i]);
}
ll t;
cin>>t;
for(ll i=1; i<=t; i++){
solve();
if(i<t) cout<<endl;
}
return 0;
}