Cod sursa(job #2915545)

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

string xword = "";
string yword = "";

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

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

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