Cod sursa(job #1143776)

Utilizator andreiagAndrei Galusca andreiag Data 15 martie 2014 23:13:07
Problema Prefix Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdio>

using namespace std;
const int Nmax = 1000005;
int K;

//char s[Nmax];
int  T[Nmax];
int len = 0;

inline int max(int x, int y) { return x > y ? x : y; }

void process(string &s)
{
    memset(T, 0, sizeof(T));
    T[0] = 0;
    len = 0;
    int i = 1, tmp = 0;
    while (i < s.size()) {
        while ((tmp > 0) && (s[i] != s[tmp]))
            tmp = T[tmp-1];

        if (s[i] == s[tmp])
            T[i] = ++tmp;
        else T[i] = 0;

        if (tmp > 0) {
            if ((i+1) % (i+1 - tmp) == 0)
                len = max(len, i+1);
        }
        //cout << i << '\t' << tmp << '\n';
        i++;
    }
}

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

    string str;
    f >> K;
    getline(f, str);
    for (int i = 0; i < K; i++) {
        getline(f, str);
        process(str);
        g << len << '\n';
    }

    return 0;
}