結果

問題 No.470 Inverse S+T Problem
ユーザー tenten
提出日時 2021-03-08 14:44:50
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,797 bytes
コンパイル時間 2,263 ms
コンパイル使用メモリ 77,780 KB
実行使用メモリ 147,492 KB
最終ジャッジ日時 2024-12-22 14:05:01
合計ジャッジ時間 19,223 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 24 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

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