Cod sursa(job #1899826)

Utilizator mihaizsZsisku mihaizs Data 2 martie 2017 22:34:10
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <iostream>
#include<string>

#include<fstream>

#define cin f
#define cout g
#include<string.h>
using namespace std;

int main()
{

    ifstream f("prefix.in");
    ofstream g("prefix.out");
    string s;
    int test;
    cin>>test;
    while(test--){
        cin>>s;
        int n = s.size();
        int* t=new int[n+4];
        memset(t, 0, (n+4)*sizeof(int));

        int cnt=0, p=2;
        t[0]=-1;
        t[1]=0;

        int maxl=0 ;
        while(p<=n){

            if(s[p-1]==s[cnt] )
            {
                cnt++;
                t[p]=cnt;

                if( 2*t[p]>=p && p%(p-t[p])==0 )
                    maxl=p;
                p++;
            }
            else if(cnt ==0){
                p++;
            }
            else{
                cnt=t[cnt];
            }

        }

        cout<<maxl<<"\n";
        delete(t);
    }

    return 0;
}