Cod sursa(job #2915561)

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

ofstream fout("lampa.out");

int n, m, xc = 1, yc = 1;
string s, xword, yword, pat = "";

void checkxy(int x, int y) {

    for (unsigned int i = 0, idx = 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() {
    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) {
                xword = s.substr(0, x);
                yword = s.substr(m - y);
            } else {
                yword = s.substr(0, y);
                xword = s.substr(m - y - x, x);
            }
            checkxy(x, y);
        }
}

void fib() {
    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");
    fin >> n >> m;
    fin >> s;
    fin.close();

    fib();
    findxy();

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