結果

問題 No.470 Inverse S+T Problem
ユーザー paruki
提出日時 2016-12-20 01:22:45
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,913 bytes
コンパイル時間 2,217 ms
コンパイル使用メモリ 180,696 KB
実行使用メモリ 6,656 KB
最終ジャッジ日時 2024-12-22 13:14:44
合計ジャッジ時間 3,288 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 3
other AC * 12 WA * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
const string ALPHA="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

int N;
string U[100001];
map<string, int> ms;
set<string> us;
int def[52];
string x[52], y[52];

void ng() {
    cout << "Impossible" << endl;
    exit(0);
}

void f(int cu) {
    if(cu == 52) {
        if(sz(us) == N) {
            rep(i, N)cout << x[i] << ' ' << y[i] << endl;
            exit(0);
        } else {
            return;
        }
    }
    
    int non = 1;
    char c = ALPHA[cu];
    us.insert(string(1, c));
    rep(i, N) if(!def[i]){
        string &u = U[i];
        def[i] = 1;
        if(u[0] == c) {
            non = 0;
            x[i] = u.substr(0, 1);
            y[i] = u.substr(1, 2);
            if(us.count(y[i]) + us.count(x[i]) == 0) {
                us.insert(y[i]);
                f(cu + 1);
                us.erase(y[i]);
            }
        }
        if(u[2] == c) {
            non = 0;
            x[i] = u.substr(0, 2);
            y[i] = u.substr(2, 1);
            if(us.count(x[i]) + us.count(y[i]) == 0) {
                us.insert(x[i]);
                f(cu + 1);
                us.erase(x[i]);
            }
        }
        def[i] = 0;
    }
    us.erase(string(1, c));
    f(cu + 1);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> N;
    rep(i, N)cin >> U[i];

    if(N > 52) {
        ng();
    }

    f(0);
    ng();
}
0