結果

問題 No.2745 String Swap Battle
ユーザー tentententen
提出日時 2024-04-23 14:38:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 578 ms / 2,000 ms
コード長 2,916 bytes
コンパイル時間 2,890 ms
コンパイル使用メモリ 85,260 KB
実行使用メモリ 57,812 KB
最終ジャッジ日時 2024-04-23 14:38:54
合計ジャッジ時間 8,412 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 186 ms
44,340 KB
testcase_01 AC 191 ms
44,480 KB
testcase_02 AC 76 ms
38,068 KB
testcase_03 AC 74 ms
37,732 KB
testcase_04 AC 82 ms
37,964 KB
testcase_05 AC 77 ms
38,344 KB
testcase_06 AC 184 ms
43,412 KB
testcase_07 AC 193 ms
43,764 KB
testcase_08 AC 177 ms
43,384 KB
testcase_09 AC 177 ms
43,236 KB
testcase_10 AC 137 ms
42,752 KB
testcase_11 AC 113 ms
41,128 KB
testcase_12 AC 571 ms
57,244 KB
testcase_13 AC 566 ms
57,812 KB
testcase_14 AC 578 ms
57,184 KB
testcase_15 AC 199 ms
44,628 KB
testcase_16 AC 209 ms
44,788 KB
testcase_17 AC 205 ms
45,556 KB
testcase_18 AC 78 ms
38,256 KB
testcase_19 AC 74 ms
38,176 KB
testcase_20 AC 71 ms
38,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        String[] mystrings = new String[n];
        TreeMap<String, Integer> counts = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            mystrings[i] = getChange(sc.next());
            counts.put(mystrings[i], counts.getOrDefault(mystrings[i], 0) + 1);
        }
        int score = n + 1 - counts.firstEntry().getValue();
        String result = Arrays.stream(mystrings).map(x -> String.valueOf(x.equals(counts.firstKey()) ? score : 0))
            .collect(Collectors.joining("\n"));
        System.out.println(result);
    }
    
    static String getChange(String s) {
        char[] arr = s.toCharArray();
        List<Deque<Integer>> idxes = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            idxes.add(new ArrayDeque<>());
        }
        for (int i = 0; i < arr.length; i++) {
            idxes.get(arr[i] - 'a').add(i);
        }
        boolean enable = false;
        boolean[] visited = new boolean[26];
        for (int i = 0; i < arr.length - 1 && !enable; i++) {
            int current = arr[i] - 'a';
            if (visited[current]) {
                continue;
            }
            for (int j = 0; j < current && !enable; j++) {
                if (idxes.get(j).size() > 0) {
                    enable = true;
                    int next = idxes.get(j).pollLast();
                    char tmp = arr[i];
                    arr[i] = arr[next];
                    arr[next] = tmp;
                }
            }
            visited[current] = true;
            idxes.get(current).pollFirst();
        }
        if (!enable) {
            int left = arr.length - 2;
            int right = arr.length - 1;
            char tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
        return new String(arr);
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0