Pagini recente » Cod sursa (job #2945173) | Cod sursa (job #920854) | Cod sursa (job #3182860) | Cod sursa (job #2557108) | Cod sursa (job #1439669)
#include <vector>
#include <fstream>
#include <string>
using namespace std;
// pe scurt, calculam functia de esec pentru algoritmul Knuth - Morris - Pratt
// folosind informatia lui, deducem rezultatul
// de exemplu, sirul
// axaxbybyaxaxbyby ar da sirul asociat(functia de esec e un sir):
// 1012000012345678
// raspunsul se deduce din faptul ca la pozitia 16 avem 8 :)
void add_to_prefix_function(const int poz, const string& str, vector<int>& v){
int jumper = v[poz-1];
while(jumper != 0 && str[poz] != str[jumper]){
jumper = v[jumper-1]; }
if(str[poz] == str[jumper]){
++jumper; }
v[poz] = jumper; }
int find_rez(const string& str){
vector<int> v(str.size(), 0);
for(int i = 1; i < str.size(); ++i){
add_to_prefix_function(i, str, v); }
for(int i = v.size(); i >= 1; --i){
if(v[i-1] && i % (i-v[i-1]) == 0){
return i; } }
return 0; }
int main(){
ifstream f("prefix.in");
ofstream g("prefix.out");
int n;
f >> n >> ws;
string str;
str.reserve(1000000);
for(int i = 0; i < n; ++i){
str.clear();
f >> str;
g << find_rez(str) << '\n'; }
return 0; }