Cod sursa(job #2771421)

Utilizator OvidRata Ovidiu Ovid Data 27 august 2021 11:48:59
Problema Prefix Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll

int t, n, m, k, a[300010], q, l, r;

int pi[1000005];

int kmp(int  pi[], string &s){
int res=0;
pi[0]=0;
int len=s.length();
for(int i=1, j=pi[0]; i<len; i++){
    //j=pi[i-1];
    while( j>0 && s[j]!=s[i] ){
        j=pi[j-1];
    }
    if(s[j]==s[i]){
        j++;
    }
    pi[i]=j;
    if( (pi[i]>0) && (pi[i]%( (i+1)-pi[i]))==0 ){
        res=max(res, i+1);
    }
}

return res;
}




ifstream fin("prefix.in"); ofstream fout("prefix.out");
#define cin fin
#define cout fout


int32_t main(){
INIT
cin>>t;

while(t--){
    string s;
    cin>>s;
    cout<<kmp(pi, s)<<"\n";
}

return 0;
}