PAT 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 (<=10000) 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 (<=1000) 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

关键词的输入: 先输入一个字符串, 再用cin.get()吸收一个空格或换行符, 若cin.get()吸收的是换行符,则表示关键词输入结束
注意点: 输入书的id时候, 换行符并没有被吸收, 需要先用cin.get将其吸收掉, 否则会被当做第一个字符串被getline()吸收
当测试点比较大的时候, 如果按值传递, 会有比较多的时间用来复制参数, 应该用引用传递, 避免不必要的复制, 否则最后一个测试点会超时

 1 #include<iostream>
 2 #include<map>
 3 #include<set>
 4 #include<string>
 5 using namespace std;
 6 
 7 void query(map<string, set<int> >& mmap, string& s){
 8   if(mmap.find(s)==mmap.end()) cout<<"Not Found"<<endl;
 9   else{
10     set<int>::iterator it=mmap[s].begin();
11     for(; it!=mmap[s].end(); it++) printf("%07d
", *it);
12   }
13 }
14 int main(){
15   int n, i;
16   string s;
17   cin>>n;
18   map<string, set<int> > Aut, Pub, Key, Tit, Year;
19   for(i=0; i<n; i++){
20     int id;
21     cin>>id;
22     cin.get();
23     getline(cin, s);
24     Tit[s].insert(id);
25     getline(cin, s);
26     Aut[s].insert(id);
27     while(cin>>s){
28       Key[s].insert(id);
29       char ch=cin.get();
30       if(ch=='
') break;
31     }
32     getline(cin, s);
33     Pub[s].insert(id);
34     getline(cin, s);
35     Year[s].insert(id);
36   }
37   cin>>n;
38   for(i=0; i<n; i++){
39     int idx;
40     scanf("%d: ", &idx);
41     getline(cin, s);
42     cout<<idx<<": "<<s<<endl;
43     if(idx==1) query(Tit, s);
44     else if(idx==2) query(Aut, s);
45     else if(idx==3) query(Key, s);
46     else if(idx==4) query(Pub, s);
47     else query(Year, s);
48   }
49   return 0;
50 }
原文地址:https://www.cnblogs.com/mr-stn/p/9360803.html