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
不得不说自己基础太差了,做了将近70分钟,用的比较笨的方法,也没啥方法,主要是cin,getline,的读取和对key的读取我弄了半天,其实也没怎么弄,全用getline,然后翻了一下算法笔记,大神就是厉害= =,这种方法确实好用,我一直期望的就是这种自己做完一道题目,然后再看一下别人更合理,更有技巧的做法,但是有些题目着实恶心。。
通过这道题比较熟练的掌握了cin,getline的读取规则,然而你要是用比较简洁,巧妙的做法,首先自己的基本功要更扎实,所以没有办法,只能慢慢学会吧。
我自己再总结一下
cin 以 空格和换行为截至标志
getline 以换行 ,(有参数,可以修改,貌似)
所以你一个cin,一个getline,一定要用getchar 去掉换行
然后cin以空格,可以用getchar把空格 或者换行 读取,以此来判断题目中的key,这一点很重要,也就是基本功的体现
别人整理的
代码1 算法笔记
#include<iostream>
#include<algorithm>
#include<set>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
map<string,set<int> > mptitle,mpauthor,mpkey,mppublisher,mppubyear;
void query(map<string,set<int> >&m,string str ){
if(m.find(str) == m.end()) cout<<"Not Found"<<endl;
else{
set<int>::iterator it ;
for(it = m[str].begin();it!=m[str].end();it++){
printf("%07d\n",*it);
}
}
}
int main(){
int n,m,id;
string title,author,key,publisher,pubyear;
cin>>n;
for(int i=0;i<n;i++){
cin>>id;
getchar();
getline(cin,title);
mptitle[title].insert(id);
getline(cin,author);
mpauthor[author].insert(id);
while(cin>>key){
mpkey[key].insert(id);
char c = getchar();
if(c == '\n') break;
}
getline(cin,publisher);
mppublisher[publisher].insert(id);
getline(cin,pubyear);
mppubyear[pubyear].insert(id);
}
cin>>m;
int type;
string t;
while(m--){
scanf("%d: ",&type);
getline(cin,t);
printf("%d: %s\n",type,t.c_str());
if(type == 1) query(mptitle,t);
else if(type == 2) query(mpauthor,t);
else if(type == 3) query(mpkey,t);
else if(type == 4) query(mppublisher,t);
else if(type == 5) query(mppubyear,t);
}
return 0;
}
自己写的
#include<iostream>
#include<algorithm>
#include<set>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
typedef struct{
string id;
string title;
string author;
vector<string> key;
string publisher;
string publishyear;
}book1;
book1 book[10005];
int main(){
int n,m;
cin>>n;
getchar();
for(int i=0;i<n;i++){
string temp;
getline(cin,temp);
book[i].id = temp;
getline(cin,temp);
book[i].title = temp;
getline(cin,temp);
book[i].author = temp;
getline(cin,temp);
string t;
for(int j=0;j<temp.length();j++){
if(temp[j] != ' ') t+=temp[j];
else{
book[i].key.push_back(t);
t.clear();
}
}
book[i].key.push_back(t);
getline(cin,temp);
book[i].publisher = temp;
getline(cin,temp);
book[i].publishyear = temp;
}
cin>>m;
string t;
getchar();
while(m--){
set<string> ans;
getline(cin,t);
int type = t[0] - '0';
string tt;
//for(int i=3;i<t.length();i++) tt+=t[i];
tt = t.substr(3,t.length() -3);
cout<<t<<endl;
if(type == 1){
for(int i=0;i<n;i++){
if(book[i].title == tt) ans.insert(book[i].id);
}
}
else if(type == 2){
for(int i=0;i<n;i++){
if(book[i].author== tt) ans.insert(book[i].id);
}
}
else if(type == 3){
for(int i=0;i<n;i++){
for(int j=0;j<book[i].key.size();j++){
if(book[i].key[j] == tt){
ans.insert(book[i].id);
break;
}
}
}
}
else if(type == 4){
for(int i=0;i<n;i++){
if(book[i].publisher == tt) ans.insert(book[i].id);
}
}
else if(type == 5){
for(int i=0;i<n;i++){
if(book[i].publishyear == tt) ans.insert(book[i].id);
}
}
if(ans.size() == 0) cout<<"Not Found"<<endl;
else{
set<string>::iterator it;
for(it = ans.begin();it != ans.end() ; it++){
cout<<*it<<endl;
}
}
}
return 0;
}