Cod sursa(job #2100867)

Utilizator HumikoPostu Alexandru Humiko Data 6 ianuarie 2018 14:35:39
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <bits/stdc++.h>
#define MAX 1000002
using namespace std;

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

char s[MAX];
int pi[MAX];

void kmp (int n)
{
    for ( int i = 2; i <= n; ++i )
    {
        pi[i] = pi[i-1];
        while ( pi[i] && s[i] != s[pi[i]+1] )
            pi[i] = pi[pi[i]];
        if ( s[i] == s[pi[i]+1] )
            pi[i]++;
    }
}

void prefix (int n)
{
    for ( int i = n; i >= 2; --i )
        if ( pi[i] && (i%(i-pi[i]) == 0) )
        {
            fout<<i<<'\n';
            return;
        }
    fout<<0<<'\n';
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);cout.tie(NULL);
    int t;
    fin>>t;
    while (t--)
    {
       fin>>(s+1);
       int n = strlen(s+1);
       kmp(n);
       prefix(n);
    }

}