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 (≤10
4
) 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
#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
struct Book{
string id;
string title;
string author;
string keywords;
string publisher;
string year;
};
bool cmp(Book b1, Book b2){
return b1.id < b2.id;
}
int main(){
int n;
scanf("%d", &n);
getchar();
Book book[n];
for(int i=0; i<n; i++){
getline(cin,book[i].id);
getline(cin,book[i].title);
getline(cin,book[i].author);
getline(cin,book[i].keywords);
getline(cin,book[i].publisher);
getline(cin,book[i].year);
}
sort(book, book+n, cmp);
int k;
scanf("%d", &k);
getchar();
for(int i=0; i<k; i++){
int flag = 0;
string s, cho, str;
getline(cin,s);
cho = s.substr(0,1);
if(cho == "1"){
str = s.substr(3,s.size()-3);
cout << "1: " << str << '\n';
for(int j=0; j<n; j++){
if(book[j].title == str){
cout << book[j].id << '\n';
flag = 1;
}
}
if(!flag){
printf("Not Found\n");
}
}else if(cho == "2"){
str = s.substr(3,s.size()-3);
cout << "2: " << str << '\n';
for(int j=0; j<n; j++){
if(book[j].author == str){
cout << book[j].id << '\n';
flag = 1;
}
}
if(!flag){
printf("Not Found\n");
}
}else if(cho == "3"){
str = s.substr(3,s.size()-3);
cout << "3: " << str << '\n';
for(int j=0; j<n; j++){
if(book[j].keywords.find(str) != -1){
cout << book[j].id << '\n';
flag = 1;
}
}
if(!flag){
printf("Not Found\n");
}
}else if(cho == "4"){
str = s.substr(3,s.size()-3);
cout << "4: " << str << '\n';
for(int j=0; j<n; j++){
if(book[j].publisher == str){
cout << book[j].id << '\n';
flag = 1;
}
}
if(!flag){
printf("Not Found\n");
}
}else if(cho == "5"){
str = s.substr(3,s.size()-3);
cout << "5: " << str << '\n';
for(int j=0; j<n; j++){
if(book[j].year == str){
cout << book[j].id << '\n';
flag = 1;
}
}
if(!flag){
printf("Not Found\n");
}
}
}
return 0;
}
题干很长的一道题目,本来想用map,但是没找到什么好的思路,就直接用纯暴力字符串模拟了。
开始交上去一直说格式错误,但是我测试样例没问题啊,看了半天才发现是Not Found后面没换行,因为题目的样例Not Found就是在最后输出的,一下子没注意。
看了书上的map<string,set>解法,学到了学到了,按书上解法再去码一遍了。
附按书上思路写的代码
#include <cstdio>
#include <iostream>
#include <string>
#include <set>
#include <map>
using namespace std;
map<string,set<int> > title, author, keyword, publisher, year;
void query(map<string,set<int> > &m, string &s){
if(m.find(s) == m.end()){
printf("Not Found\n");
}else{
for(set<int>::iterator it=m[s].begin(); it!=m[s].end(); it++){
printf("%07d\n", *it);
}
}
}
int main(){
int n;
scanf("%d", &n);
for(int i=0; i<n; i++){
int id;
string biaoti, zuozhe, guanjian, chuban, nian;
scanf("%d", &id);
getchar();
getline(cin,biaoti);
title[biaoti].insert(id);
getline(cin,zuozhe);
author[zuozhe].insert(id);
char c;
while(cin >> guanjian){
keyword[guanjian].insert(id);
c = getchar();
if(c == '\n') break;
}
getline(cin,chuban);
publisher[chuban].insert(id);
getline(cin,nian);
year[nian].insert(id);
}
int k;
scanf("%d", &k);
for(int i=0; i<k; i++){
int num;
scanf("%d: ", &num);
string s;
getline(cin,s);
switch(num){
case 1:{
cout << "1: " << s << endl;
query(title,s);
break;
}
case 2:{
cout << "2: " << s << endl;
query(author,s);
break;
}
case 3:{
cout << "3: " << s << endl;
query(keyword,s);
break;
}
case 4:{
cout << "4: " << s << endl;
query(publisher,s);
break;
}
case 5:{
cout << "5: " << s << endl;
query(year,s);
break;
}
}
}
return 0;
}