-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game Of strings - KMP
52 lines (49 loc) · 945 Bytes
/
Game Of strings - KMP
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
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define elif else if
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
string ss;
int pf[1000005];
int mxp=0;
void kmp(int pos)
{
int i,k=0;
pf[0]=0;
for(i=1;i<ss.size();i++)
{
while(k && ss[i]!=ss[k])
k=pf[k-1];
if(ss[i]==ss[k])
k++;
pf[i]=k;
if(i<=pos) mxp=max(mxp,pf[i]);
}
}
int main()
{
freopen("in","r",stdin);
freopen("out","w",stdout);
int t;
cin>>t;
while(t--)
{
int i,j,k,pos;
cin>>ss>>pos;
assert(1<=ss.size() && ss.size()<=1000000);
assert(1<=pos && pos<=ss.size());
pos--;
mxp=0;
memset(pf,0,sizeof(pf));
kmp(pos);
k=pf[ss.size()-1];
while(k>mxp && k) k=pf[k-1];
if(k==0) cout<<"Puchi is a cheat!";
else for(i=0;i<k;i++) cout<<ss[i];
cout<<"\n";
}
return 0;
}