题目描述
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.
翻译:一个数字图书馆包括上千万本书,通过它们的书名、作者、关键字摘要、出版商和出版时间进行存储。每本书都有一个唯一的7位数字作为它的ID。给你任意一个读者的查询,你需要输出对应的书籍,按照它们的编号升序排序。
INPUT FORMAT
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
翻译:每个输入文件包括一组输入数据。对于每组输入数据,第一行包括一个正整数N(<=10000) 代表书籍的总数。接下来的N个信息块,每一个都代表一本书的信息,包括以下六行:
第一行:7位数字
第二行:书名-不超过80个字符
第三行:作者-不超过80个字符
第四行:关键字-每个关键字都是不超过10个字符的没有空格的,关键字由空格分隔
第五行:出版商-不超过80个字符
第六行:出版时间-一个在 [1000, 3000]之间的4位数字
假设每本书都只有一个作者,关键字不超过五个,总共不超过1000个关键字,不超过1000个出版商
在书籍信息后面一行是一个正整数M(<=1000),代表读者的查询个数。接下来的M行,每行都是以下格式之一:
1: 书名
2:作者名
3:关键字
4:出版商名
5:一个代表年份的4位数字
OUTPUT FORMAT
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.
翻译:对于每个查询,首先输出查询内容,接下来升序输出目标书籍的ID,每个一行。如果没有对应的书,则输出”Not Found”。
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将书号插入所有相关的信息中,查询时直接排序输出即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#define INF 99999999
using namespace std;
map<string,vector<string> >mp;
int N,M;
int main(){
string num,name,author,keyword,publisher,year;
cin>>N;
scanf("\n");
for(int i=0;i<N;i++){
getline(cin,num),scanf("\n");
getline(cin,name),scanf("\n"),mp[name].push_back(num);
getline(cin,author),scanf("\n"),mp[author].push_back(num);
while(cin.peek() != '\n'){
cin>>keyword,mp[keyword].push_back(num);
}
scanf("\n");
getline(cin,publisher),scanf("\n"),mp[publisher].push_back(num);
getline(cin,year),scanf("\n"),mp[year].push_back(num);
}
cin>>M;
string s;
int temp;
for(int i=0;i<M;i++){
scanf("%d: ",&temp);
getline(cin,s);
cout<<temp<<": "<<s<<endl;
sort(mp[s].begin(),mp[s].end());
for(int i=0;i<mp[s].size();i++)
cout<<mp[s][i]<<endl;
if(mp[s].size()==0)cout<<"Not Found"<<endl;
scanf("\n");
}
return 0;
}