Cod sursa(job #941053)

Utilizator andreiagAndrei Galusca andreiag Data 17 aprilie 2013 20:31:35
Problema Prefix Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <string>
#include <iostream>
#include <cstdio>

using namespace std;

int process_line(string A) {
    int ret = 0;
    int period = 0;
    int cnd = 0;
    int pos = 1;
    while(pos < A.size()) {
        if (A[cnd] == A[pos])
            {if (cnd == 0) {period = pos;}
             else if (pos % period == 0) ret = pos;
             cnd++;pos++;}
        else {if (cnd == 0) pos++;
              else if (pos % period == 0) ret = pos; cnd = 0;}
    }
    return ret;
}

int main()
{
    freopen("prefix.in", "r", stdin);
    freopen("prefix.out", "w", stdout);

    int T;
    int res;
    string line;
    scanf("%d\n", &T);

    for(int i = 0; i < T; i++) {
        getline(cin, line); //cout << line << endl;
        res = process_line(line + ' ');
        printf("%d\n", res);
    }
    return 0;
}