Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4. public class Solution {
  5.  
  6. public static void main(String[] args) {
  7. /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
  8. Scanner s = new Scanner(System.in);
  9. int n = s.nextInt();
  10. s.nextLine();
  11. List<String> birds = new ArrayList<>(n);
  12. while (n-- > 0) {
  13. birds.add(s.nextLine());
  14. }
  15.  
  16. Map<Character, Integer> counts = new HashMap<>();
  17. for (String bird : birds) {
  18. for (char c : bird.toLowerCase().toCharArray()) {
  19. if (c < 'a' || c > 'z') {
  20. continue;
  21. }
  22. if (!counts.containsKey(c)) {
  23. counts.put(c, 0);
  24. }
  25. counts.put(c, counts.get(c) + 1);
  26. }
  27. }
  28.  
  29. List<Character> chars = new ArrayList<Character>(counts.keySet());
  30. chars.sort((a, b) -> Integer.compare(counts.get(a), counts.get(b)));
  31.  
  32. Map<Character, Integer> vals = new HashMap<>();
  33. int val = 10;
  34. int lastCount = counts.get(chars.get(0));
  35. for (char c : chars) {
  36. int count = counts.get(c);
  37. if (count != lastCount) {
  38. val--;
  39. if (val == 0) {
  40. break;
  41. }
  42. lastCount = count;
  43. }
  44. vals.put(c, val);
  45. }
  46.  
  47. Map<String, Integer> scores = new HashMap<>();
  48. for (String bird : birds) {
  49. int score = 0;
  50. for (char c : bird.toLowerCase().toCharArray()) {
  51. if (vals.containsKey(c)) {
  52. score += vals.get(c);
  53. }
  54. }
  55. scores.put(bird, score);
  56. }
  57.  
  58. birds.sort((a, b) -> {
  59. int diff = Integer.compare(scores.get(b), scores.get(a));
  60. if (diff == 0) {
  61. diff = a.compareTo(b);
  62. }
  63.  
  64. return diff;
  65. });
  66.  
  67. for (String bird : birds) {
  68. System.out.println(bird);
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement