Cod sursa(job #2915548)

Utilizator alin.gabrielAlin Gabriel Arhip alin.gabriel Data 23 iulie 2022 09:59:34
Problema Lampa Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

vector<string> xwords;
vector<string> ywords;

void findxy(int xc, int yc, int n, int m, string s) {
    for (int x = ceil((m - yc) / xc); x >= 1; x--) {
        if ((m - (xc * x)) % yc == 0) {
            int y = (m - (xc * x)) / yc;
            if (n % 2 == 1) {
                string fxword = s.substr(0, x);
                string lxword = s.substr(m - y - x, x);
                if (fxword == lxword) {
                    string yword = s.substr(m - y);
                    xwords.push_back(fxword);
                    ywords.push_back(yword);
                }
            } else {
                string fyword = s.substr(0, y);
                string lyword = s.substr(m - y);
                if (fyword == lyword) {
                    string xword = s.substr(m - y - x, x);
                    xwords.push_back(xword);
                    ywords.push_back(fyword);
                }
            }
        }
    }
}

int fib(int n) {
    int a = 1, b = 1;
    if (n < 3)
        return a;
    while (n-- > 2) {
        int tmp = a + b;
        a = b;
        b = tmp;
    }
    return b;
}

int main() {
    ifstream fin("lampa.in");
    ofstream fout("lampa.out");

    int n, m;
    string s;
    fin >> n >> m;
    fin >> s;

    int xc = fib(n - 2);
    int yc = fib(n - 1);
    findxy(xc, yc, n, m, s);

    if (xwords.empty())
        fout << 0;
    else {
        string xword = xwords[0];
        string yword = ywords[0];
        for (unsigned int i = 1 ; i < xwords.size() ; i++)
            if (xword.compare(xwords[i]) > 0) {
                xword = xwords[i];
                yword = ywords[i];
            }
        fout << xword << "\n";
        fout << yword;
    }

    fin.close();
    fout.close();
    return 0;
}