Cod sursa(job #1925810)

Utilizator giotoPopescu Ioan gioto Data 13 martie 2017 18:45:18
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <cstdio>
#include <cstring>
using namespace std;

int t, pi[1000005];
char a[1000005];
int main()
{
    freopen("prefix.in", "r", stdin);
    freopen("prefix.out", "w", stdout);
    scanf("%d", &t);
    for(int tr = 1; tr <= t ; ++tr){
        scanf("%s", a + 1);
        int L = strlen(a + 1);
        int q = 0; pi[1] = 0;
        for(int i = 2; i <= L ; ++i){
            while(q != 0 && a[i] != a[q + 1])
                q = pi[q];
            if(a[i] == a[q + 1]) ++q;
            pi[i] = q;
        }
        int i = L;
        while(i > 0 && !(pi[i] > 0 && i % (i - pi[i]) == 0))
            --i;
        printf("%d\n", i);
    }
    return 0;
}