Cod sursa(job #2735510)

Utilizator SergiuS3003Sergiu Stancu Nicolae SergiuS3003 Data 2 aprilie 2021 14:59:34
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f ( "prefix.in" );
ofstream g ( "prefix.out" );
const int NMAX = 1000002;
int p[NMAX];
char s[NMAX];
int main()
{
    int T;
    f >> T;

    while ( T-- )
    {
        f >> ( s + 1 ) ;
        int n = strlen ( s + 1 );
        p[1] = 0;
        int k = 0, ans = 0;

        for ( int j = 2; j <= n; j++ )
        {
            while ( k != 0 && s[j] != s[k + 1] )
                k = p[k];

            if ( s[j] == s[k + 1] )
                k++;

            p[j] = k;

            if ( k > 0 && j % ( j - k ) == 0 )
                ans = j;
        }

        g << ans << '\n';
    }

    return 0;
}