Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.util.stream.Collectors;
- public class Solution {
- public static void main(String[] args) {
- /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
- Scanner s = new Scanner(System.in);
- int n = s.nextInt();
- s.nextLine();
- List<String> birds = new ArrayList<>(n);
- while (n-- > 0) {
- birds.add(s.nextLine());
- }
- Map<Character, Integer> counts = new HashMap<>();
- for (String bird : birds) {
- for (char c : bird.toLowerCase().toCharArray()) {
- if (c < 'a' || c > 'z') {
- continue;
- }
- if (!counts.containsKey(c)) {
- counts.put(c, 0);
- }
- counts.put(c, counts.get(c) + 1);
- }
- }
- List<Character> chars = new ArrayList<Character>(counts.keySet());
- chars.sort((a, b) -> Integer.compare(counts.get(a), counts.get(b)));
- Map<Character, Integer> vals = new HashMap<>();
- int val = 10;
- int lastCount = counts.get(chars.get(0));
- for (char c : chars) {
- int count = counts.get(c);
- if (count != lastCount) {
- val--;
- if (val == 0) {
- break;
- }
- lastCount = count;
- }
- vals.put(c, val);
- }
- Map<String, Integer> scores = new HashMap<>();
- for (String bird : birds) {
- int score = 0;
- for (char c : bird.toLowerCase().toCharArray()) {
- if (vals.containsKey(c)) {
- score += vals.get(c);
- }
- }
- scores.put(bird, score);
- }
- birds.sort((a, b) -> {
- int diff = Integer.compare(scores.get(b), scores.get(a));
- if (diff == 0) {
- diff = a.compareTo(b);
- }
- return diff;
- });
- for (String bird : birds) {
- System.out.println(bird);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement