結果

問題 No.470 Inverse S+T Problem
ユーザー tenten
提出日時 2020-12-24 13:44:32
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 2,417 bytes
コンパイル時間 2,366 ms
コンパイル使用メモリ 79,292 KB
実行使用メモリ 140,000 KB
最終ジャッジ日時 2024-12-22 14:04:12
合計ジャッジ時間 19,351 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 24 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static HashSet<Integer> used = new HashSet<>();
    static int[] base;
    static int[] lefts;
    static int[] rights;
    static int n;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        base = new int[n];
        lefts = new int[n];
        rights = new int[n];
        for (int i = 0; i < n; i++) {
            base[i] = getNum(sc.next().toCharArray());
        }
        check(0);
        System.out.println("Impossible");
    }
    
    static void check(int idx) {
        if (idx == n) {
            output();
            System.exit(0);
        }
        lefts[idx] = base[idx] / (54 * 54);
        rights[idx] = base[idx] % (54 * 54);
        if (!used.contains(lefts[idx]) && !used.contains(rights[idx])) {
            used.add(lefts[idx]);
            used.add(rights[idx]);
            check(idx + 1);
            used.remove(lefts[idx]);
            used.remove(rights[idx]);
        }
        lefts[idx] = base[idx] / 54;
        rights[idx] = base[idx] % 54;
        if (!used.contains(lefts[idx]) && !used.contains(rights[idx])) {
            used.add(lefts[idx]);
            used.add(rights[idx]);
            check(idx + 1);
            used.remove(lefts[idx]);
            used.remove(rights[idx]);
        }
    }
    
    static void output() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(getStr(lefts[i])).append(" ").append(getStr(rights[i])).append("\n");
        }
        System.out.print(sb);
    }
    
    static String getStr(int x) {
        StringBuilder sb = new StringBuilder();
        if (x < 54) {
            sb.append(getChar(x));
        } else {
            sb.append(getChar(x / 54)).append(getChar(x % 54));
        }
        return sb.toString();
    }
    
    static char getChar(int x) {
        if (x <= 26) {
            return (char)(x + 'A' - 1);
        } else {
            return (char)(x - 27 + 'a' - 1);
        }
    }
    
    static int getNum(char[] arr) {
        int value = 0;
        for (char c : arr) {
            value *= 54;
            value += getNum(c);
        }
        return value;
    }
    
    static int getNum(char c) {
        if (c <= 'Z') {
            return c - 'A' + 1;
        } else {
            return 27 + c - 'a' + 1;
        }
    }
    
}
0