結果

問題 No.470 Inverse S+T Problem
ユーザー tentententen
提出日時 2021-03-08 14:44:50
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,797 bytes
コンパイル時間 3,988 ms
コンパイル使用メモリ 79,492 KB
実行使用メモリ 86,196 KB
最終ジャッジ日時 2023-08-24 01:43:36
合計ジャッジ時間 12,425 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
59,780 KB
testcase_01 AC 122 ms
55,992 KB
testcase_02 AC 124 ms
55,392 KB
testcase_03 AC 122 ms
55,572 KB
testcase_04 AC 125 ms
55,660 KB
testcase_05 AC 122 ms
55,420 KB
testcase_06 AC 413 ms
65,268 KB
testcase_07 AC 778 ms
75,108 KB
testcase_08 AC 509 ms
65,148 KB
testcase_09 AC 125 ms
55,468 KB
testcase_10 AC 135 ms
55,476 KB
testcase_11 AC 340 ms
64,660 KB
testcase_12 AC 126 ms
55,524 KB
testcase_13 AC 128 ms
55,468 KB
testcase_14 AC 159 ms
55,368 KB
testcase_15 AC 175 ms
58,228 KB
testcase_16 AC 126 ms
55,492 KB
testcase_17 AC 141 ms
55,740 KB
testcase_18 AC 135 ms
55,208 KB
testcase_19 AC 178 ms
58,212 KB
testcase_20 AC 122 ms
55,260 KB
testcase_21 AC 127 ms
55,376 KB
testcase_22 AC 126 ms
55,612 KB
testcase_23 AC 129 ms
55,404 KB
testcase_24 AC 131 ms
55,568 KB
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static String[] strings;
    static int n;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        strings = new String[n];
        for (int i = 0; i < n; i++) {
            strings[i] = sc.next();
        }
        check(0, new HashSet<String>(), new boolean[n]);
        System.out.println("Impossible");
    }
    
    static void check(int idx, HashSet<String> used, boolean[] firstOne) {
        if (idx == n) {
            output(firstOne);
            System.exit(0);
        }
        String one = strings[idx].substring(0, 1);
        String two = strings[idx].substring(1, 3);
        if (!used.contains(one) && !used.contains(two)) {
            firstOne[idx] = true;
            used.add(one);
            used.add(two);
            check(idx + 1, used, firstOne);
            used.remove(one);
            used.remove(two);
        }
        one = strings[idx].substring(0, 2);
        two = strings[idx].substring(2, 3);
        if (!used.contains(one) && !used.contains(two)) {
            firstOne[idx] = false;
            used.add(one);
            used.add(two);
            check(idx + 1, used, firstOne);
            used.remove(one);
            used.remove(two);
        }
    }
    
    static void output(boolean[] firstOne) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            if (firstOne[i]) {
                sb.append(strings[i].substring(0, 1)).append(" ").append(strings[i].substring(1, 3));
            } else {
                sb.append(strings[i].substring(0, 2)).append(" ").append(strings[i].substring(2, 3));
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
}
0