Pagini recente » Cod sursa (job #2727840) | Cod sursa (job #3180466) | Cod sursa (job #3040977) | Cod sursa (job #2828517) | Cod sursa (job #2201578)
// 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;
}