結果

問題 No.470 Inverse S+T Problem
ユーザー nebukuro09nebukuro09
提出日時 2016-12-20 00:42:27
言語 D
(dmd 2.109.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,464 bytes
コンパイル時間 1,850 ms
コンパイル使用メモリ 145,920 KB
実行使用メモリ 16,128 KB
最終ジャッジ日時 2024-06-12 05:59:58
合計ジャッジ時間 7,660 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 21 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.ascii;

int N;
int[] ans;
string[] U;
bool[string] used;

bool dfs(int p) {
    if (p > N - 1)
        return true;

    string s0 = U[p][0].to!string;
    string s12 = U[p][1..3].to!string;
    string s01 = U[p][0..2].to!string;
    string s2 = U[p][2].to!string;
    if (!used[s0] && !used[s12]) {
        used[s0] = true;
        used[s12] = true;
        ans[p] = 0;
        if (dfs(p+1))
            return true;
        used[s0] = false;
        used[s12] = false;
    }
    if (!used[s01] && !used[s2]) {
        used[s01] = true;
        used[s2] = true;
        ans[p] = 1;
        if (dfs(p+1))
            return true;
        used[s01] = false;
        used[s2] = false;
    }
    return false;
}

void main() {
    N = readln.chomp.to!int;

    ans = new int[](N);
    U = new string[](N);
    foreach (i; 0..N)
        U[i] = readln.chomp;

    foreach (c1; letters) {
        used[c1.to!string] = false;
        foreach (c2; letters)
            used[c1.to!string ~ c2.to!string] = false;
    }

    if (dfs(0)) {
        foreach (i; 0..N) {
            if (ans[i] == 0)
                writeln(U[i][0], " ", U[i][1..3]);
            else
                writeln(U[i][0..2], " ", U[i][2]);
        }
    }
    else
        writeln("Impossible");

}
0