Cod sursa(job #2915558)

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

int xc = 1, yc = 1;
string xword, yword;
string pat = "ab";

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

    int idx = 0;
    for (unsigned int i = 0 ; i < pat.length() ; i++) {
        if (pat[i] == 'a') {
            if (s.compare(idx, x, xword) != 0) 
                return false;
            idx += x;
        } else {
            if (s.compare(idx, y, yword) != 0) 
                return false;
            idx += y;
        }
    }

    return true;
}

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, x, y)) {
                        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, x, y)) {
                        xword = fxword;
                        yword = fyword;
                        break;
                    }
                }
            }
        }
}

void fib(int n) {
    int x = 1, y = 1;
    string a = "a", b = "b";
    string aux;
    if (n < 3)
        return;
    for (int i = 3 ; i <= n ; i++) {
        int tmp = x + y;
        aux = a + b;
        x = y;
        y = tmp;
        a = b;
        b = aux;
        if (i == n - 2) 
            xc = y;
        if (i == n - 1)
            yc = y;
    }
    pat = aux;
}

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

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

    fib(n);
    findxy(xc, yc, n, m, s);

    if (xword.empty())
        fout << 0;
    else {
        fout << xword << "\n";
        fout << yword;
    }

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