Cod sursa(job #1886645)

Utilizator tziplea_stefanTiplea Stefan tziplea_stefan Data 21 februarie 2017 00:31:54
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <fstream>
#define VAL 1000005

using namespace std;

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

int T, N, i, j, mx;
int FF[VAL], poz;
string P;

int main()
{
    fin >> T;
    for (i=1; i<=T; i++)
    {
        fin >> P;
        N=P.size();
        mx=0;
        P='*'+P+'*';
        for (j=2; j<=N; j++)
        {
            poz=FF[j-1];
            while (poz>0 && P[j]!=P[poz+1])
              poz=FF[poz];
            if (P[j]==P[poz+1])
              poz++;
            FF[j]=poz;
            if (FF[j]!=0 && (FF[j] % (j-FF[j])==0))
              mx=j;
        }
        fout << mx << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}