Cod sursa(job #2460584)

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

using namespace std;

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

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

int main()
{
    f >> t;
    while(t--)
        {   f >> s;
            memset(phi, 0, sizeof(phi));
            int n = s.size();
            for(int i=1; i<n; i++)
                {   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;
                }
            //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;
}