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
Note1
- 思路:给每一个关键词都对应到id,创立map, map里的second的类型为set 递增id排列
- 一边鲁完的代码,根本跑不了 输入有问题,getline 和 cin , scanf吃回车, 为了调这个bug导致oled显示的代码忘记消除又搞了很长时间
- 留着看看吧, 好丑
Code1
#include<iostream>
#include<set>
#include<string>
#include<map>
using namespace std;
map <string, set<int> > title, author, keyword, publisher, year;
int main(){
int num;
cin << num;
for(int i = 0; i < num; i++){
int tempid;
string temptitle, tempauthor, tempkeyword, temppublisher, tempyear;
cin >> tempid >> temptitle >> tempauthor >> tempkeyword >> temppublisher >> tempyear;
title[temptitle].insert(tempid);
author[tempauthor].insert(tempid);
keyword[tempkeyword].insert(tempid);
publisher[temppublisher].insert(tempid);
year[tempyear].insert(tempid);
}
int temtnum, tempclass;
string temp;
cin << tempnum;
for(int i = 0; i < tempnum; i++){
scanf("%d: %s", tempclass, temp);
switch(tempclass){
case 1: {
if(title.find(temp) != title.end()){
for(auto it = title[temp].begin(); it != tile[temp].end(); it++){
printf("%07d\n", *it);
}
}
else << cout" Not Found" << endl;
break;
}
case 2:{
if(author.find(temp) != author.end()){
for(auto it = author[temp].begin(); it != author[temp].end(); it++){
printf("%07d\n", *it);
}
}
else << cout" Not Found" << endl;
break;
}
case 3:{
if(keyword.find(temp) != keyword.end()){
for(auto it = keyword[temp].begin(); it != keyword[temp].end(); it++){
printf("%07d\n", *it);
}
}
else << cout" Not Found" << endl;
break;
}
case 4:{
if(publisher.find(temp) != publisher.end()){
for(auto it = publisher[temp].begin(); it != publisher[temp].end(); it++){
printf("%07d\n", *it);
}
}
else << cout" Not Found" << endl;
break;
}
case 5:{
if(year.find(temp) != year.end()){
for(auto it = year[temp].begin(); it != year[temp].end(); it++){
printf("%07d\n", *it);
}
}
else << cout" Not Found" << endl;
break;
}
default: {cout << cout" Not Found" << endl; break;}
}
}
return 0;
}
Note2
- 改成了ac代码,还是很丑
- 注意关键词一行里面有多个,若果直接getline(cin, tempkeyword)的话, 后期无法同个单个关键字找到id 解决方法:whihile(1)人工分开
- 需要人工分开的原因:cin scanf碰到空格和回车后自动断开,把空格和回车ascll码放入缓冲区,如果接下来继续用cin scanf本身有去除开头空格的机制所以没关系,但是后面如果跟着的是getline(cin, string), gets()则把/n读入跳过getline和gets
Code2
#include<iostream>
#include<set>
#include<string>
#include<map>
using namespace std;
map <string, set<int> > title, author, keyword, publisher, year;
int main(){
int num;
cin >> num;
for(int i = 0; i < num; i++){
int tempid;
string temptitle, tempauthor, tempkeyword, temppublisher, tempyear;
scanf("%d", &tempid);
getchar();
getline(cin, temptitle);
getline(cin, tempauthor);
while(1) {
cin >> tempkeyword;
keyword[tempkeyword].insert(tempid);
char c = getchar();
if(c == '\n') break;
}
getline(cin, temppublisher);
getline(cin, tempyear);
title[temptitle].insert(tempid);
author[tempauthor].insert(tempid);
publisher[temppublisher].insert(tempid);
year[tempyear].insert(tempid);
}
int tempnum, tempclass;
cin >> tempnum;
for(int i = 0; i < tempnum; i++){
scanf("%d: ", &tempclass);
string temp;
getline(cin, temp);
cout << tempclass << ": " << temp << "\n";
switch(tempclass){
case 1: {
if(title.find(temp) != title.end()){
for(auto it = title[temp].begin(); it != title[temp].end(); it++){
printf("%07d\n", *it);
}
}
else cout << "Not Found" << endl;
break;
}
case 2:{
if(author.find(temp) != author.end()){
for(auto it = author[temp].begin(); it != author[temp].end(); it++){
printf("%07d\n", *it);
}
}
else cout << "Not Found" << endl;
break;
}
case 3:{
if(keyword.find(temp) != keyword.end()){
for(auto it = keyword[temp].begin(); it != keyword[temp].end(); it++){
printf("%07d\n", *it);
}
}
else cout << "Not Found" << endl;
break;
}
case 4:{
if(publisher.find(temp) != publisher.end()){
for(auto it = publisher[temp].begin(); it != publisher[temp].end(); it++){
printf("%07d\n", *it);
}
}
else cout << "Not Found" << endl;
break;
}
case 5:{
if(year.find(temp) != year.end()){
for(auto it = year[temp].begin(); it != year[temp].end(); it++){
printf("%07d\n", *it);
}
}
else cout << "Not Found" << endl;
break;
}
default: {cout << "Not Found" << endl; break;}
}
}
return 0;
}