結果

問題 No.470 Inverse S+T Problem
ユーザー face4face4
提出日時 2019-11-22 18:17:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,784 bytes
コンパイル時間 632 ms
コンパイル使用メモリ 72,992 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 01:37:41
合計ジャッジ時間 2,236 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 1 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 9 ms
4,376 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 最悪計算量は確実にTLEするレベルだが
// 実際は問題ない(と思いたい)
#include<iostream>
#include<vector>
using namespace std;
#define inRange(x,a,b) (a <= x && x <= b)

int main(){
    int n;
    cin >> n;
    if(n > 26){
        cout << "Impossible" << endl;
        return 0;
    }
    auto enc = [](string &s)->int{
        int ret = 0;
        for(int i = s.length()-1; i >= 0; i--){
            ret *= 52;
            if(inRange(s[i],'a','z')){
                ret += s[i]-'a';
            }else{
                ret += 26 + s[i]-'A';
            }
        }
        if(s.length() == 2) ret += 52;
        return ret;
    };
    auto f = [](int i, string &s)->pair<string,string>{
        if(i == 0)  return {s.substr(0,1), s.substr(1,2)};
        else        return {s.substr(0,2), s.substr(2,1)};
    };
    vector<string> v(n);
    for(int i = 0; i < n; i++)  cin >> v[i];
    bool used[3000] = {};
    for(int i = 0; i < 1<<n; i++){
        bool ng = false;
        for(int j = 0; j < n; j++){
            pair<string,string> p = f((i>>j)&1, v[j]);
            int a = enc(p.first), b = enc(p.second);
            if(used[a] || used[b]){
                while(--j >= 0){
                    p = f((i>>j)&1, v[j]);
                    int a = enc(p.first), b = enc(p.second);
                    used[a] = used[b] = false;
                }
                ng = true;
                break;
            }else{
                used[a] = used[b] = true;
            }
        }
        if(ng)  continue;

        // ok!
        for(int j = 0; j < n; j++){
            pair<string,string> p = f((i>>j)&1, v[j]);
            cout << p.first << " " << p.second << endl;
        }
        return 0;
    }
    cout << "Impossible" << endl;
    return 0;
}
0