Cod sursa(job #2672602)

Utilizator sabinandreiBocan Sabin Andrei sabinandrei Data 14 noiembrie 2020 11:41:17
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.54 kb
// Tema Excelenta.cpp : This file contains the 'main' function. Program execution begins and ends there.
//


#include <fstream>
#include <cstring>

using namespace std;

ifstream in("prefix.in");
ofstream out("prefix.out");

int t, n;
int pr[1000002];
char s[1000002];

void prefix() {
    for (int i = 2, p = 0; i <= n; ++i) {
        while (p > 0 && s[p + 1] != s[i])
            p = pr[p];

        if (s[p + 1] == s[i])
            ++p;
        pr[i] = p;
    }
}

int main() {
    s[0] = ' ';
    in >> t;
    for (int q = 1; q <= t; ++q) {
        in >> (s + 1);
        n = strlen(s);
        prefix();
        bool ok = 0;
        for (int j = n; j >= 1; --j)
            if (pr[j] && j % (j - pr[j]) == 0) {
                out << j << "\n";
                j = 1;
                ok = 1;
            }
        if (ok == 0)
            out << 0 << "\n";
    }
    return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file