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 (≤104) 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
法一:
将书籍的标题、作者、关键字、出版社、出版时间等信息封装为一个结构体,书籍编号作为map的关键字,信息结构体作为map的第二项,构建map容器Library,后期根据查找要求,遍历Library查找符合要求的书籍,输出书籍编号。这种方法的查找效率有点低,即便将cout改为printf,最后一个测试点需要尝试三四次才有一次通过。
代码一:
#include <iostream>
#include <map>
#include <set>
#include <cstdio>
using namespace std;
struct book{
string title,author,publisher,publishyear;
set<string> keyword;
};
int main(){
int N;
cin>>N;
map<string,struct book> library;
getchar();
while(N--){
struct book B;
string ID,str;
getline(cin,ID);
getline(cin,str);
B.title=str;
getline(cin,str);
B.author=str;
getline(cin,str);
int i;
string keyword;
keyword.clear();
for(i=0;i<str.size();i++){
if(str[i]!=' '){
keyword+=str[i];
if(i==str.size()-1){
B.keyword.insert(keyword);
keyword.clear();
}
}
else {
B.keyword.insert(keyword);
keyword.clear();
}
}
getline(cin,str);
B.publisher=str;
getline(cin,str);
B.publishyear=str;
library[ID]=B;
// cout<<ID<<" "<<B.title<<" "<<B.author<<" "<<B.publisher<<" "<<B.publishyear<<endl;
}
cin>>N;
while(N--){
int k;
string str;
cin>>k;
getchar();
getchar();
getline(cin,str);
printf("%d: %s\n",k,str.c_str());
map<string,struct book>::iterator t;
set<string>::iterator tt;
int flag=0,IsFound=0;
for(t=library.begin();t!=library.end();t++){
flag=0;
switch(k){
case 1 :
if(t->second.title==str)
flag=1;
break;
case 2 :
if(t->second.author==str)
flag=1;
break;
case 3 :
for(tt=t->second.keyword.begin();tt!=t->second.keyword.end();tt++)
if(*tt==str){
flag=1;
break;
}
break;
case 4 :
if(t->second.publisher==str)
flag=1;
break;
default:
if(t->second.publishyear==str)
flag=1;
break;
}
if(flag){
printf("%s\n",t->first.c_str());
IsFound=1;
}
}
if(!IsFound)
printf("Not Found\n");
}
return 0;
}
法二:
为了提高查找效率,可以利用map容器内部的红黑树特性,将N缩减为logN。具体做法为设置一个map数组,每个元素存储一种书籍信息,第一项为对应信息的字符串,第二项为队列,存储符合这一字符串的书籍编号,找到符合要求的字符串,输出所有书籍编号。
代码二:
#include <iostream>
#include <map>
#include <set>
#include <cstdio>
using namespace std;
int main(){
int N;
cin>>N;
map<string,set<string> >library[5];
getchar();
while(N--){
int i;
string ID,str;
getline(cin,ID);
for(i=0;i<5;i++){
getline(cin,str);
if(i!=2)
library[i][str].insert(ID);
else {
string keyword;
keyword.clear();
int j;
for(j=0;j<str.size();j++){
if(str[j]!=' '){
keyword+=str[j];
if(j==str.size()-1){
library[2][keyword].insert(ID);
keyword.clear();
}
}
else {
library[2][keyword].insert(ID);
keyword.clear();
}
}
}
}
}
cin>>N;
while(N--){
int k;
string str;
cin>>k;
getchar();
getchar();
getline(cin,str);
printf("%d: %s\n",k,str.c_str());
int i;
if(library[k-1].find(str)!=library[k-1].end()){
set<string>::iterator t;
for(t=library[k-1][str].begin();t!=library[k-1][str].end();t++)
printf("%s\n",(*t).c_str());
}
else
printf("Not Found\n");
}
return 0;
}