Created
February 3, 2013 18:36
-
-
Save msg555/4703032 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <cstring> | |
#include <map> | |
#include <set> | |
#include <queue> | |
#include <cstdio> | |
using namespace std; | |
void pcase(int t) { | |
cout << "Case #" << t << ": "; | |
} | |
int N, M; | |
bool cn[110][110]; | |
bool vis[110]; | |
bool dfs(int x, int snk) { | |
if(x == snk) return true; | |
if(vis[x]) return false; | |
vis[x] = true; | |
for(int i = 0; i <= snk; i++) { | |
if(cn[x][i] && dfs(i, snk)) { | |
swap(cn[x][i], cn[i][x]); | |
return true; | |
} | |
} | |
return false; | |
} | |
bool can(const string& A, const string& B) { | |
int src = 2 * M; | |
int snk = 2 * M + 1; | |
int buck = N / M; | |
memset(cn, 0, sizeof(cn)); | |
for(int i = 0; i < M; i++) { | |
cn[src][i] = cn[M + i][snk] = true; | |
for(int j = 0; j < M; j++) { | |
bool ok = true; | |
for(int k = 0; ok && k < buck; k++) { | |
ok = A[i * buck + k] == '?' || B[j * buck + k] == '?' || | |
A[i * buck + k] == B[j * buck + k]; | |
} | |
cn[i][M + j] = ok; | |
} | |
} | |
for(int i = 0; i < M; i++) { | |
memset(vis, 0, sizeof(vis)); | |
if(!dfs(src, snk)) return false; | |
} | |
return true; | |
} | |
int main() { | |
int T; cin >> T; | |
for(int t = 1; t <= T; t++) { | |
pcase(t); | |
cin >> M; | |
string K1, K2; cin >> K1 >> K2; | |
N = K1.size(); | |
if(!can(K1, K2)) { | |
cout << "IMPOSSIBLE" << endl; | |
continue; | |
} | |
for(int i = 0; i < N; i++) { | |
if(K1[i] != '?') continue; | |
for(char ch = 'a'; ; ch++) { | |
K1[i] = ch; | |
if(can(K1, K2)) break; | |
} | |
} | |
cout << K1 << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment