結果

問題 No.269 見栄っ張りの募金活動
ユーザー nebukuro09
提出日時 2016-12-20 00:39:09
言語 D
(dmd 2.109.1)
結果
RE  
実行時間 -
コード長 1,516 bytes
コンパイル時間 1,473 ms
コンパイル使用メモリ 146,048 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-12 05:56:48
合計ジャッジ時間 2,568 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other RE * 22
権限があれば一括ダウンロードができます

ソースコード

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;
    }
    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;
    if (N > 52 + 52*52) {
        writeln("Impossible");
        return;
    }

    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