Cod sursa(job #2915553)

Utilizator alin.gabrielAlin Gabriel Arhip alin.gabriel Data 23 iulie 2022 10:50:47
Problema Lampa Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <fstream>
#include <string>
using namespace std;

string xword;
string yword;

bool check(string xword, string yword, string s, int n) {

    for (int i = 3 ; i <= n ; i++) {
        string tmp = xword + yword;
        xword = yword;
        yword = tmp;
    }

    return (yword.compare(s) == 0);
}

void findxy(int xc, int yc, int n, int m, string s) {

    for (int x = (m - yc) / xc + 1; 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 fyword = s.substr(m - y);
                    if (check(fxword, fyword, s, n)) {
                        xword = fxword;
                        yword = fyword;
                        break;
                    }
                }
            } else {
                string fyword = s.substr(0, y);
                string lyword = s.substr(m - y);
                if (fyword == lyword) {
                    string fxword = s.substr(m - y - x, x);
                    if (check(fxword, fyword, s, n)) {
                        xword = fxword;
                        yword = fyword;
                        break;
                    }
                }
            }
        }
}

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 (xword.empty())
        fout << 0;
    else {
        fout << xword << "\n";
        fout << yword;
    }

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