Cod sursa(job #1469515)

Utilizator dorumusuroiFMI - Doru Musuroi dorumusuroi Data 8 august 2015 15:58:36
Problema Prefix Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
const char iname[] = "prefix.in";
const char oname[] = "prefix.out";
int getPrefix(const string P)
{
    int poz = 0, dim = 0;
    vector<int> pr;
    pr.push_back(-1);
    int m = P.length();
    int k = -1;
    for(int q = 1; q < m; q++)
    {
        while(k>-1 && P[k+1] != P[q])
            k = pr[k];
        if(P[k+1] == P[q])
            k++;
        if(k + 1 == 1)  poz = q;
        if((poz > 0) && (k+1 > 0) &&((k+1)%poz == 0)) dim = q+1;
        pr.push_back(k);
    }
    return dim;
}

int main()
{
    ifstream in(iname);
    ofstream out(oname);
    vector<int> pr;
    string P;
    int T;
    in >> T;
    for(int q = 0; q < T; q++)
    {
        in >> P;
        out << getPrefix(P) << '\n';;
    }
    return 0;
}