Cod sursa(job #876798)

Utilizator laurionLaurentiu Ion laurion Data 12 februarie 2013 10:07:33
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.35 kb
#ifndef INFOARENA
#define DBG
#endif
#define nume "prefix"

#include<cstdio>
#include<iostream>
#include<fstream>
#include<vector>
#include<cstring>
#include<string>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cassert>
#include<ctime>
#include<list>
#include<algorithm>

using namespace std;
using namespace __gnu_cxx;

//#define _PARSARE_
#ifdef _PARSARE_
#define DIM 8192
char ax[DIM+16];
int _idx;
template<class T>
inline void cit(T& x)
{
    x=0;
    while((ax[_idx]<'0' || ax[_idx]>'9') && (ax[_idx]!='-'))
        if(++_idx==DIM)fread(ax, 1, DIM, stdin), _idx=0;

    int neg=0;
    if(ax[_idx]=='-') {
        neg=1;
        if(++_idx==DIM)fread(ax, 1, DIM, stdin),_idx=0;
    }

    while(ax[_idx]>='0' && ax[_idx]<='9') {
        x=x*10+ax[_idx]-'0';
        if(++_idx==DIM)fread(ax,1, DIM, stdin),_idx=0;
    }
    if(neg) x=-x;
}
#else
#ifndef ONLINE_JUDGE
ifstream fin (nume ".in");
#endif
#endif //_PARSARE_
#ifndef ONLINE_JUDGE
ofstream fout(nume ".out");
#endif
#ifdef DBG
#define fout cout
#endif
//// hash-uri
//#include<ext/hash_map>
//hash_multimap<int,int> H;
//hash_map<char*,int> H;
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
#define foreach(it, v) for (typeof((v).begin()) it = (v).begin(),stop=(v).end(); it != stop; ++it)
#define foreach_r(it, v) for (typeof((v).rbegin()) it = (v).rbegin(),stop=(v).rend(); it != stop; ++it)
template<class T> inline void Swap(T& a,T& b){a^=b,b^=a,a^=b;}

int n,m;

string s;
int pi[1000000+10];
int main()
{
    #ifdef _PARSARE_
    #ifndef ONLINE_JUDGE
    freopen(nume ".in","r",stdin);
    #endif
    cit(n);
    #endif
    
    int t;
    fin>>t;
//    cerr<<
    fin.get();
//    <<'!';
    while(t--){
        getline(fin,s);
        n=s.length();
        s.insert(s.begin(),'\0');
        memset(pi,0,sizeof(pi));
        pi[1]=0;
        for(int i=2,k=0;i<=n;++i){
            while(k>0 && s[i]!=s[k+1])
                k=pi[k];
            if(s[i]==s[k+1])
                k++;
            pi[i]=k;
        }
        int i;
        for(i=n;i>=2;--i){
            if(pi[i]>=i/2 && i%(i-pi[i])==0){
                fout<<i<<'\n';
                break;
            }
        }
        if(i<2)
            fout<<0<<'\n';
    }
    
    
    #ifndef DBG
    #ifndef ONLINE_JUDGE
    fout.close();
    #endif
    #endif
    return 0;
}