Cod sursa(job #2460583)

Utilizator FrostfireMagirescu Tudor Frostfire Data 23 septembrie 2019 22:46:33
Problema Prefix Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <iostream>
#include <fstream>
#include <cstring>
#define NMAX 10000000

using namespace std;

ifstream f("prefix.in");
ofstream g("prefix.out");

int t, phi[NMAX+10];
char s[NMAX+10];

int main()
{
    f >> t;
    while(t--)
        {   f >> s;
            memset(phi, 0, sizeof(phi));
            int i=1;
            int n = strlen(s);
            phi[0] = 0;
            while(i < n)
                {   int x = i - 1;
                    while(s[phi[x]] != s[i] && phi[x]) x = phi[x]-1;
                    if(s[phi[x]] == s[i]) phi[i] = phi[x] + 1;
                    else phi[i] = 0;
                    i++;
                }
            //for(int i=0; i<strlen(s); i++) cout << phi[i] << ' ';
            //cout << '\n';

            bool ok = 0;
            for(int i=n-1; i>=0 && !ok; i--)
                if(phi[i] && phi[i] % (i + 1 - phi[i]) == 0) g << i+1 << '\n', ok = 1;
            if(!ok) g << 0 << '\n';
        }
    return 0;
}