Cod sursa(job #2547669)

Utilizator memecoinMeme Coin memecoin Data 15 februarie 2020 16:06:23
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <stack>

#define INF 0x3f3f3f3f

using namespace std;

#ifdef DEBUG
string name = "data";
#else
string name = "prefix";
#endif

ifstream fin(name + ".in");
ofstream fout(name + ".out");

int n;

string s;

int pi[1000000];

void solve() {
    fin >> s;
    
    memset(pi, 0, sizeof(pi));
    
    int j = 0;
    int startMatch = -1;
    int i = 1;
    
    int best = 0;
    
    while (i <= s.size()) {
        if (s[i] == s[j]) {
            if (startMatch == -1) {
                startMatch = i;
            }
            j++;
            pi[i] = j;
            i++;
        } else {
            if (startMatch > 0) {
                if (startMatch <= (i - startMatch)) {
                    best = max(best, i - (i % startMatch));
                }
                startMatch = -1;
            }
            if (j == 0) {
                i++;
            } else {
                j = pi[j - 1];
            }
        }
    }
    
    fout << best << "\n";
}

int main() {
    
    fin >> n;
    
    for (int i = 0; i < n; ++i) {
        solve();
    }
    
    return 0;
}