Cod sursa(job #2666170)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 1 noiembrie 2020 02:00:10
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("prefix.in");
ofstream fout("prefix.out");

const int nmax = 1000006;
int t, pi[nmax];
string x;

int main(){
    fin >> t;
    while (t--){
        fin >> x;
        int n = x.size(), ans = 0;
        for (int i = 1; i < n; ++i){
            int l = pi[i - 1];
            while (l > 0 && x[i] != x[l]) l = pi[l - 1];
            if (x[l] == x[i]) ++l;
            pi[i] = l;
            int aux = i + 1 - pi[i];
            if (pi[i] >= (i + 2) / 2  &&  (i + aux * 2 + 1) % aux == 0) ans = i + 1;
        }
        fout << ans << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}