1022 Digital Library (30 分)

1022 Digital Library (30 分)
 

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID's.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:

  • Line #1: the 7-digit ID number;
  • Line #2: the book title -- a string of no more than 80 characters;
  • Line #3: the author -- a string of no more than 80 characters;
  • Line #4: the key words -- each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
  • Line #5: the publisher -- a string of no more than 80 characters;
  • Line #6: the published year -- a 4-digit number which is in the range [1000, 3000].

It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.

After the book information, there is a line containing a positive integer M (≤) which is the number of user's search queries. Then M lines follow, each in one of the formats shown below:

  • 1: a book title
  • 2: name of an author
  • 3: a key word
  • 4: name of a publisher
  • 5: a 4-digit number representing the year

Output Specification:

For each query, first print the original query in a line, then output the resulting book ID's in increasing order, each occupying a line. If no book is found, print Not Found instead.

Sample Input:

3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla

Sample Output:

1: The Testing Book
1111111
2222222
2: Yue Chen
1111111
3333333
3: keywords
1111111
2222222
3333333
4: ZUCS Print
1111111
5: 2011
1111111
2222222
3: blablabla
Not Found
题目分析:用vector存储每个节点 再到每个节点里去寻找查询的点 我这样写只过了前2个测试点
后面用网上的用map的方法来写
vector
  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<iostream>
  3 #include<vector>
  4 #include<queue>
  5 #include<stack>
  6 #include<algorithm>
  7 #include<string>
  8 using namespace std;
  9 int flag = 0;
 10 struct Node
 11 {
 12     int id;
 13     string book_title;
 14     string author;
 15     vector<string> key_words;
 16     string publisher;
 17     string year;
 18 };
 19 bool compare(const Node& a, const Node& b)
 20 {
 21     return a.id < b.id;
 22 }
 23 void Query(int id,string query,vector<Node>&V)
 24 {
 25     switch (id)
 26     {
 27     case 1:
 28         for (auto it : V)
 29             if (it.book_title == query)
 30             {
 31                 cout << it.id << endl;
 32                 flag = 1;
 33             }
 34         break;
 35     case 2:
 36         for (auto it : V)
 37             if (it.author == query)
 38             {
 39                 cout << it.id << endl;
 40                 flag = 1;
 41             }
 42         break;
 43     case 3:
 44         for (auto it : V)
 45             for (auto i : it.key_words)
 46                 if (i == query)
 47                 {
 48                     cout << it.id << endl;
 49                     flag = 1;
 50                     break;
 51                 }
 52         break;
 53     case 4:
 54         for (auto it : V)
 55             if (it.publisher == query)
 56             {
 57                 cout << it.id << endl;
 58                 flag = 1;
 59             }
 60         break;
 61     case 5:
 62         for (auto it : V)
 63             if (it.year == query)
 64             {
 65                 cout << it.id << endl;
 66                 flag = 1;
 67             }
 68         break;
 69     }
 70 }
 71 int main()
 72 {
 73     int N;
 74     cin >> N;
 75     vector<Node> V(N);
 76     Node B;
 77     string words;
 78     for (int i = 0; i < N; i++)
 79     {
 80         scanf("%d
", &B.id);
 81         getline(cin, B.book_title);
 82         getline(cin, B.author);
 83         getline(cin, words);
 84         getline(cin, B.publisher);
 85         getline(cin, B.year);
 86         int j = 0, m = 0;
 87         for (;j < words.length(); j++)
 88         {
 89             if (words[j] != ' ')
 90                 continue;
 91             else
 92             {
 93                 B.key_words.push_back(words.substr(m, j - m));
 94                 m = j + 1;
 95             }
 96         }
 97         B.key_words.push_back(words.substr(m, j - m));
 98         V[i] = B;
 99     }
100     sort(V.begin(), V.end(), compare);
101     int M;
102     cin >> M;
103     int id;
104     string query;
105     for (int i = 0; i < M; i++)
106     {
107         flag = 0;
108         scanf("%d: ", &id);
109         getline(cin, query);
110         printf("%d: ", id);
111         cout << query << endl;
112         Query(id, query, V);
113         if (flag == 0)
114             cout << "Not Found" << endl;
115     }
116 }
View Code

map

最后一个点还是超时了

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<iostream>
 3 #include<vector>
 4 #include<queue>
 5 #include<stack>
 6 #include<algorithm>
 7 #include<string>
 8 #include<map>
 9 #include<set>
10 using namespace std;
11 map<string, set<int>> maptitle, mapauthor, mapwords, mappub, mapyear;
12 void Query(map<string, set<int>> mp, string query)
13 {
14     if (mp.find(query) == mp.end())
15     {
16         cout << "Not Found" << endl;
17         return;
18     }
19     for (auto it : mp[query])
20         printf("%07d
", it);
21 }
22 int main()
23 {
24     int N, id;
25     cin >> N;
26     string title, author, words, puber, year;
27     for (int i =0; i < N; i++)
28     {
29         cin >> id;
30         getchar();
31         getline(cin, title);
32         maptitle[title].insert(id);
33         getline(cin, author);
34         mapauthor[author].insert(id);
35         while (cin >> words)
36         {
37             mapwords[words].insert(id);
38             char c = getchar();
39             if (c == '
')
40                 break;
41         }
42         getline(cin, puber);
43         mappub[puber].insert(id);
44         getline(cin, year);
45         mapyear[year].insert(id);
46     }
47     int M;
48     cin >> M;
49     string query;
50     for (int i = 0; i < M; i++)
51     {
52         scanf("%d: ", &id);
53         getline(cin, query);
54         printf("%d: ", id);
55         cout << query<<endl;
56         if (id == 1)Query(maptitle, query);
57         else if (id == 2)Query(mapauthor, query);
58         else if (id == 3)Query(mapwords, query);
59         else if (id == 4)Query(mappub, query);
60         else
61             Query(mapyear, query);
62     }
63 }
View Code
原文地址:https://www.cnblogs.com/57one/p/11936755.html