結果

問題 No.2745 String Swap Battle
ユーザー tenten
提出日時 2024-04-23 14:38:45
言語 Java
(openjdk 23)
結果
AC  
実行時間 590 ms / 2,000 ms
コード長 2,916 bytes
コンパイル時間 2,762 ms
コンパイル使用メモリ 84,756 KB
実行使用メモリ 57,304 KB
最終ジャッジ日時 2024-10-15 14:57:05
合計ジャッジ時間 8,892 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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();
}
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0