Cod sursa(job #2201578)

Utilizator EdyOnuEdy Onu EdyOnu Data 5 mai 2018 10:42:10
Problema Prefix Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
// Prefix.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>

using namespace std;


vector <int> getLongestSufPrex(const string& str) {

	vector <int> next( static_cast<int>(str.size()) );

	for (int i = 1,k = 0; i < str.length(); ++i) {
		while (k > 0 && str[k] != str[i])k = next[k - 1];
		if (str[k] == str[i])++k;
		next[i] = k;
	}

	return next;
}

int calculateLength(const vector <int>& sufixesPrefixes) {
	
	int i;
	
	for (i = sufixesPrefixes.size() - 1; i >= 0; --i) {
		if (sufixesPrefixes[i] > 0 && (i + 1) % (i + 1 - sufixesPrefixes[i]) == 0)break;
	}

	return i + 1;
}


void readAndParse(istream& in,ostream& out) {

	int T; 

	in >> T,in.get();

	string line;

	while (T--) {
		getline(in, line);
		out << calculateLength(getLongestSufPrex(line)) << '\n';
	}
}


int main(){
	
	readAndParse(ifstream{ "prefix.in" }, ofstream{ "prefix.out" });
    return 0;
}