Cod sursa(job #2640179)

Utilizator OvidRata Ovidiu Ovid Data 5 august 2020 15:02:31
Problema Prefix Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 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

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



int t, n, m, k, a[300010], q, l, r;
char s[1000010];
int pi[1000010];


int32_t main(){
INIT
cin>>t;
cin.get();
while(t--){
    cin.getline(s, 1000000); n=strlen(s);
    pi[0]=0;
    int ml=0;

    for(int i=1; i<n; i++){

        int l0=pi[i-1];
        while(s[l0]!=s[i] && l0>0){
            l0=pi[l0-1];
        }
        if(s[i]==s[l0]){
            l0++; pi[i]=l0;
        }
        else{
            pi[i]=0;
        }

        if((pi[i]>0) &&( ((i+1)%((i+1)-pi[i]) )==0 ) ){
            ml=i+1;
        }


    }
    cout<<ml<<"\n";
}



return 0;
}