Cod sursa(job #1520757)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 9 noiembrie 2015 13:06:36
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <cstdio>
#include <cstring>

using namespace std;

const int MAX_LEN = 1000000;

char s[2 + MAX_LEN];
int pi[1 + MAX_LEN];

int main() {
    freopen("prefix.in", "r", stdin);
    freopen("prefix.out", "w", stdout);

    int t, i, n, q;

    scanf("%d\n", &t);
    while(t--) {
        gets(s + 1);
        n = strlen(s + 1);
        for(i = 2, pi[1] = 0, q = 0; i <= n; i++) {
            while(q > 0 && s[q + 1] != s[i]) q = pi[q];
            if(s[q + 1] == s[i]) q++;
            pi[i] = q;
        }
        for(i = n; i && (pi[i] == 0 || (i % (i - pi[i])) != 0); i--);
        printf("%d\n", i);
    }

    return 0;
}