解法一:倒排索引

遍历全部Book查找会非常慢,使用 Map 在单词和书本 id 之间建立倒排索引加速查找。

  1. import java.io.*;
  2. import java.util.*;
  3. public class Main {
  4. public static void main(String[] args) throws IOException {
  5. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  6. PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
  7. int N = Integer.parseInt(in.readLine());
  8. Map<String, List<String>> titieMap = new HashMap<>();
  9. Map<String, List<String>> authorMap = new HashMap<>();
  10. Map<String, List<String>> keyWordMap = new HashMap<>();
  11. Map<String, List<String>> publisherMap = new HashMap<>();
  12. Map<Integer, List<String>> yearMap = new HashMap<>();
  13. Book[] books = new Book[N];
  14. for (int i = 0; i < N; ++i) {
  15. books[i] = new Book(
  16. in.readLine(),
  17. in.readLine(),
  18. in.readLine(),
  19. in.readLine(),
  20. in.readLine(),
  21. Integer.parseInt(in.readLine())
  22. );
  23. }
  24. Arrays.sort(books);
  25. List<String> tmp;
  26. for (Book book : books) {
  27. tmp = titieMap.getOrDefault(book.title, new ArrayList<>());
  28. tmp.add(book.id);
  29. titieMap.put(book.title, tmp);
  30. tmp = authorMap.getOrDefault(book.author, new ArrayList<>());
  31. tmp.add(book.id);
  32. authorMap.put(book.author, tmp);
  33. for (String keyword : book.keyWords) {
  34. tmp = keyWordMap.getOrDefault(keyword, new ArrayList<>());
  35. tmp.add(book.id);
  36. keyWordMap.put(keyword, tmp);
  37. }
  38. tmp = publisherMap.getOrDefault(book.publisher, new ArrayList<>());
  39. tmp.add(book.id);
  40. publisherMap.put(book.publisher, tmp);
  41. tmp = yearMap.getOrDefault(book.year, new ArrayList<>());
  42. tmp.add(book.id);
  43. yearMap.put(book.year, tmp);
  44. }
  45. int M = Integer.parseInt(in.readLine());
  46. for (int i = 0; i < M; ++i) {
  47. String query = in.readLine();
  48. out.println(query);
  49. String request = query.substring(3);
  50. List<String> ans;
  51. switch (query.charAt(0)) {
  52. case '1':
  53. ans = titieMap.get(request);
  54. break;
  55. case '2':
  56. ans = authorMap.get(request);
  57. break;
  58. case '3':
  59. ans = keyWordMap.get(request);
  60. break;
  61. case '4':
  62. ans = publisherMap.get(request);
  63. break;
  64. case '5':
  65. ans = yearMap.get(Integer.parseInt(request));
  66. break;
  67. default:
  68. ans = null;
  69. }
  70. if (ans == null) {
  71. out.println("Not Found");
  72. } else {
  73. for (String s : ans) {
  74. out.println(s);
  75. }
  76. }
  77. }
  78. out.flush();
  79. }
  80. }
  81. class Book implements Comparable {
  82. String id;
  83. String title;
  84. String author;
  85. Set<String> keyWords;
  86. String publisher;
  87. int year;
  88. Book(String id, String title, String author, String keyWords, String publisher, int year) {
  89. this.id = id;
  90. this.title = title;
  91. this.author = author;
  92. this.keyWords = new HashSet<>(Arrays.asList(keyWords.split(" ")));
  93. this.publisher = publisher;
  94. this.year = year;
  95. }
  96. @Override
  97. public String toString() {
  98. return "Book{" +
  99. "id=" + id +
  100. ", title='" + title + '\'' +
  101. ", author='" + author + '\'' +
  102. ", keyWords=" + keyWords +
  103. ", publisher='" + publisher + '\'' +
  104. ", year=" + year +
  105. '}';
  106. }
  107. @Override
  108. public int compareTo(Object o) {
  109. Book book = (Book) o;
  110. return id.compareTo(book.id);
  111. }
  112. }