Cod sursa(job #135655)

Utilizator dominoMircea Pasoi domino Data 14 februarie 2008 02:37:00
Problema Lampa Scor Ascuns
Compilator cpp Status done
Runda Marime 1.39 kb
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#define FIN "lampa.in"
#define FOUT "lampa.out"
#define MAX_S 3100000
#define sz(x) ((int)(x).size())

int N, M;
string C, S, bstA, bstB;

int works(const char a[], const char b[])
{
    int i, p = 0, n = strlen(a), m = strlen(b);

    for (i = 0; i < sz(C); ++i)
        if (C[i] == 'A')
        {
            if (memcmp(S.c_str()+p, a, n)) return 0;
            p += n;
        } 
        else 
        {
            if (memcmp(S.c_str()+p, b, m)) return 0;
            p += m;
        }
    return 1;

}

int main(void)
{
    int i, j, a, b;
    string A, B;

    ifstream fin(FIN, ios::in); 
    ofstream fout(FOUT, ios::out);

    fin >> N >> M >> S;

    A = "A"; B = "B";
    for (i = 3; i <= N; ++i)
    {
        C = A+B;
        A = B; B = C;
        if (i == N-2) a = sz(C);
        if (i == N-1) b = sz(C);
    }

    for (i = 1; i*a <= M; ++i)
    {
        j = M-i*a;
        if (j%b) continue;
        j /= b;
        A = N&1 ? S.substr(0, i) : S.substr(j, i);
        B = N&1 ? S.substr(i, j) : S.substr(0, j);
        if (!works(A.c_str(), B.c_str())) continue;
        if (bstA == "" || bstA > A)
            bstA = A, bstB = B;
    }
    if (bstA == "")
        fout << 0 << endl;
    else
        fout << bstA << endl << bstB << endl;

    return 0;
}