Cod sursa(job #1821722)

Utilizator mihai.alphamihai craciun mihai.alpha Data 3 decembrie 2016 15:15:38
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <cstdio>
#include <cctype>
FILE *fin, *fout;

char c[1000010];
int p[1000010];

#define BUF 1 << 17
char buf[BUF];
int pos = 1 << 17;
inline char next()  {
    if(pos == BUF)
        fread(buf, 1, BUF, fin),
        pos = 0;
    return buf[pos++];
}

inline void r(int &x)  {
    int semn = 1;
    char ch;
    x = 0;
    while(!isdigit(ch) && ch != '-')
        ch = next();
    if(ch == '-')
        ch = next(), semn = -1;
    while(isdigit(ch))  {
        x = x * 10 + ch - '0';
        ch = next();
    }
}
int main()  {
    fin = fopen("prefix.in", "r");
    fout = fopen("prefix.out", "w");
    int n;
    char ch;
    r(n);
    int j;
    for(j = 0;j < n;j++)  {
        while(!isalpha(ch))
            ch = next();
        int nr = 1;
        while(isalpha(ch))  {
            c[nr] = ch;
            ch = next();
            nr++;
        }
        p[1] = 0;
        int k = 0;
        int ans = 0;
        for(int i = 2;i <= nr;i++)  {
            while(k > 0 && c[k + 1] != c[i])
                k = p[k];
            if(c[k + 1] == c[i])
                k++;
            p[i] = k;
            if(p[i] && i % (i - p[i]) == 0)
                ans = i;
        }
        fprintf(fout, "%d\n", ans);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}