Cod sursa(job #1855234)

Utilizator DobosDobos Paul Dobos Data 23 ianuarie 2017 15:34:12
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <bits/stdc++.h>
#define NMAX 1000005

using namespace std;

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

string V;
int KMP[NMAX];

inline void prefix(){
    int k = 0;
    for(int i = 2; i < V.size(); i++){
        while(k && V[k + 1] != V[i])
            k = KMP[k];
        if(V[k + 1] == V[i])
            k++;
        KMP[i] = k;
    }

}

int main()
{
    ios :: sync_with_stdio(false);
    fin.tie(NULL);

    int T,i;
    fin >> T;
    getline(fin,V);
    while(T--){

       getline(fin,V);
       V = ' ' + V;
       prefix();
       for( i = V.size() - 1; i >= 1; i--){
            if(i%(i - KMP[i]) == 0 && KMP[i]){
                fout << i << "\n";
                i = -1;
            }
       }
       if(i == 0)
            fout << 0 << "\n";


    }
    return 0;
}