Pagini recente » Cod sursa (job #485997) | Cod sursa (job #156309) | Cod sursa (job #1920103) | Cod sursa (job #981412) | Cod sursa (job #1474357)
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>
#define DIM 3100000
#define MAXINDEX 30
#define ERROR_MESSAGE "0"
#define infile "lampa.in"
#define outfile "lampa.out"
using namespace std;
ifstream fin(infile);
ofstream fout(outfile);
int wordLength, wordIndex;
char word[DIM];
int firstSeqApparitionCount[MAXINDEX], secondSeqApparitionCount[MAXINDEX];
string wordCode;
struct Solution {
vector<char> firstWord;
vector<char> secondWord;
} solution;
int compareWords(vector<char> firstWord, vector<char> secondWord) {
if (firstWord.size() > secondWord.size()) {
secondWord.resize(firstWord.size());
}
else {
firstWord.resize(secondWord.size());
}
for (int index = 0; index < firstWord.size(); ++index) {
if (firstWord[index] > secondWord[index]) {
return 1;
}
else if (secondWord[index] > firstWord[index]) {
return -1;
}
}
return 0;
}
void tryLengths(int firstWordLength, int secondWordLength) {
int firstWordStartIndex, secondWordStartIndex = wordLength - secondWordLength + 1;
if (wordIndex % 2 == 0) {
firstWordStartIndex = secondWordLength + 1;
}
else {
firstWordStartIndex = 1;
}
for (int positionInCode = 0, index = 1; positionInCode < wordCode.size(); ++positionInCode) {
char currChar = wordCode[positionInCode];
if (currChar == 'A') {
for (int tempIndex = index; tempIndex <= index + firstWordLength - 1; ++tempIndex) {
if (word[tempIndex] != word[firstWordStartIndex + tempIndex - index]) {
return;
}
}
index += firstWordLength;
}
else {
for (int tempIndex = index; tempIndex <= index + secondWordLength - 1; ++tempIndex) {
if (word[tempIndex] != word[secondWordStartIndex + tempIndex - index]) {
return;
}
}
index += secondWordLength;
}
}
vector<char> firstWord, secondWord;
for (int index = firstWordStartIndex; index <= firstWordStartIndex + firstWordLength - 1; ++index) {
firstWord.push_back(word[index]);
}
for (int index = secondWordStartIndex; index <= secondWordStartIndex + secondWordLength - 1; ++index) {
secondWord.push_back(word[index]);
}
if (solution.firstWord.empty() || compareWords(firstWord, solution.firstWord) < 0) {
solution.firstWord = firstWord;
solution.secondWord = secondWord;
}
}
int main() {
fin >> wordIndex >> wordLength;
fin >> word + 1;
firstSeqApparitionCount[1] = secondSeqApparitionCount[2] = 1;
string firstWordCode = "A", secondWordCode = "B";
for (int index = 3; index <= wordIndex; ++index) {
firstSeqApparitionCount[index] = firstSeqApparitionCount[index - 2] + firstSeqApparitionCount[index - 1];
secondSeqApparitionCount[index] = secondSeqApparitionCount[index - 2] + secondSeqApparitionCount[index - 1];
wordCode = firstWordCode + secondWordCode;
firstWordCode = secondWordCode;
secondWordCode = wordCode;
}
for (int firstWordLength = 1; firstSeqApparitionCount[wordIndex] * firstWordLength < wordLength; ++firstWordLength) {
int secondWordLength = wordLength - firstSeqApparitionCount[wordIndex] * firstWordLength;
if (secondWordLength % secondSeqApparitionCount[wordIndex] != 0)
continue;
secondWordLength /= secondSeqApparitionCount[wordIndex];
tryLengths(firstWordLength, secondWordLength);
}
if (solution.firstWord.empty()) {
fout << ERROR_MESSAGE;
}
else {
for (vector<char>::iterator it = solution.firstWord.begin(); it != solution.firstWord.end(); ++it)
fout << *it;
fout << "\n";
for (vector<char>::iterator it = solution.secondWord.begin(); it != solution.secondWord.end(); ++it)
fout << *it;
}
return 0;
}
//Trust me, I'm the Doctor!
//Miriam e tare!