Cod sursa(job #2915560)

Utilizator alin.gabrielAlin Gabriel Arhip alin.gabriel Data 23 iulie 2022 12:18:41
Problema Lampa Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <fstream>
#include <string>
using namespace std;

ofstream fout("lampa.out");

int xc = 1, yc = 1;
string pat = "";

void 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;
            idx += x;
        } else {
            if (s.compare(idx, y, yword) != 0) 
                return;
            idx += y;
        }
    }

    fout << xword << "\n";
    fout << yword;
    fout.close();
    exit(0);
}

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

    for (int x = 1; x * xc <= 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);
                if (s.compare(m - y - x, x, fxword) == 0) {
                    string fyword = s.substr(m - y);
                    check(fxword, fyword, s, n, x, y);
                }
            } else {
                string fyword = s.substr(0, y);
                if (s.compare(m - y, y, fyword) == 0) {
                    string fxword = s.substr(m - y - x, x);
                    check(fxword, fyword, s, n, x, y);
                }
            }
        }
}

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

int main() {
    ifstream fin("lampa.in");
    int n, m;
    string s;
    fin >> n >> m;
    fin >> s;
    fin.close();

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

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