Files
Java/作业/刘航宇-day45/第三题/P2975.java
T
2025-12-28 21:05:06 +08:00

39 lines
1.2 KiB
Java

import java.util.*;
public class P2975 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
StringBuilder all = new StringBuilder();
if (in.hasNextLine()) {
all.append(in.nextLine());
while (in.hasNextLine()) {
all.append('\n');
all.append(in.nextLine());
}
}
Map<Character, Integer> cnt = new HashMap<>();
for (int i = 0; i < all.length(); i++) {
char c = all.charAt(i);
if (c == '\r' || c == '\n') continue;
cnt.put(c, cnt.getOrDefault(c, 0) + 1);
}
char bestChar = 0;
int best = -1;
for (Map.Entry<Character, Integer> e : cnt.entrySet()) {
char c = e.getKey();
int v = e.getValue();
if (v > best || (v == best && c < bestChar)) {
best = v;
bestChar = c;
}
}
if (best < 0) {
System.out.println();
System.out.println(0);
} else {
System.out.println(bestChar);
System.out.println(best);
}
in.close();
}
}