#include #include #include #include #include #include #include using namespace std; char word[2][110]; char c[100010]; int K, N, X, Y; int diff[110]; bool cross(int a, int b) { if (a <= b) { return a + X > b; } else { return b + Y > a; } } vector, int> > found[2]; int mod26(int a) { assert(a < 100); while(a >= 26) a-= 26; return a; } void wynajdz(int dlugosc, int a) { int m = strlen(word[a]); for(int i=0; i+m <= N; i++) { for(int j=0; j tmp(diff, diff+dlugosc); found[a].push_back(make_pair(tmp, i)); } } } string wynik; bool cokolwiek; string generate(const vector& cykl) { string res = ""; int pos = 0; for(int i=0; i& cykl) { string lol = generate(cykl); if (cokolwiek) { if (lol != wynik){ printf("ambiguous\n"); //printf("%s %s\n",lol.c_str(), wynik.c_str()); return false; } } else { cokolwiek = true; wynik = lol; } return true; } int main() { while(true) { cokolwiek = false; scanf("%d",&K); if (K == 0) return 0; scanf("%s %s %s", word[0], word[1], c); X = strlen(word[0]); Y = strlen(word[1]); N = strlen(c); for(int dl = 1; dl <= K; dl++) { found[0].clear(); found[1].clear(); wynajdz(dl, 0); wynajdz(dl, 1); sort(found[0].begin(), found[0].end()); sort(found[1].begin(), found[1].end()); int i = 0, j = 0; while(i < found[0].size() && j < found[1].size()) { if (found[0][i].first == found[1][j].first && !cross(found[0][i].second, found[1][j].second)) { if (!check(found[0][i].first)) { goto end; } i++; j++; } else if (found[0][i] < found[1][j]) { i++; } else { j++; } } } if (cokolwiek) printf("%s\n", wynik.c_str()); else printf("impossible\n"); end:; } return 0; }