Cod sursa(job #3163546)

Utilizator iulia_morariuIulia Ela Morariu iulia_morariu Data 31 octombrie 2023 16:53:41
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;

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

int main(){
    cin.tie(0);ios::sync_with_stdio(0);

    int t; fin >> t;
    for(int ii = 0; ii < t; ii++){

        string v; fin >> v;
        int n = v.size();

        int p[n];
        p[0] = 0;
        int k = 0;
        for(int i = 1; i < n; i++){
            while(k > 0 && v[i] != v[k]){
                k = p[k - 1];
            }
            if(v[i] == v[k]) k++;
            p[i] = k;
        }

        int it = 0;
        for(int i = 0; i < n; i++){
            if( (i + 1) % ( (i + 1) - p[i] ) == 0 && p[i] != 0){
                it = i + 1;
            }
        }

        fout << it << endl;
    }

    return 0;
}