fork download
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.HashSet;
  9. import java.util.Iterator;
  10. import java.util.List;
  11.  
  12. /* https://w...content-available-to-author-only...c.net/problem/1302 */
  13. public class Main {
  14. public static void main(String[] args) throws IOException {
  15.  
  16. int n = Integer.parseInt(br.readLine());
  17.  
  18. HashSet<String> bookHashList = new HashSet<String>();
  19. List<String> bookList = new ArrayList<String>();
  20.  
  21. for (int i = 0; i < n; i++) {
  22. String book = br.readLine();
  23. bookList.add(book);
  24. bookHashList.add(book);
  25. }
  26.  
  27. int[] bookHashListIndex = new int[bookHashList.size()];
  28. int index = 0;
  29. int maxIndex = 0;
  30.  
  31. Iterator<String> iterator = bookHashList.iterator();
  32. while (iterator.hasNext()) {
  33. String book = iterator.next().toString();
  34. for (int i = 0; i < bookList.size(); i++) {
  35. if (bookList.get(i).equals(book)) {
  36. bookHashListIndex[index]++;
  37. }
  38. }
  39.  
  40. index++;
  41. }
  42.  
  43. for (int i = 0; i < bookHashListIndex.length; i++) {
  44. if (bookHashListIndex[i] >= maxIndex) {
  45. maxIndex = bookHashListIndex[i];
  46. }
  47. }
  48.  
  49. int indexMatcher = 0;
  50. iterator = bookHashList.iterator();
  51. List<String> bookSortedList = new ArrayList<String>();
  52. while (iterator.hasNext()) {
  53. if (bookHashListIndex[indexMatcher] == maxIndex) {
  54. bookSortedList.add(iterator.next());
  55. }
  56.  
  57. indexMatcher++;
  58.  
  59. if (indexMatcher == bookHashListIndex.length) {
  60. break;
  61. }
  62. }
  63.  
  64. Collections.sort(bookSortedList);
  65.  
  66. bw.write(bookSortedList.get(0));
  67.  
  68. bw.flush();
  69. bw.close();
  70. br.close();
  71. }
  72. }
  73.  
Success #stdin #stdout 0.08s 48124KB
stdin
10
dc
zxo
abd
pa
ccc
dc
sad
zxcvdq
qteq
abd
stdout
pa