1022. Digital Library (30)

来源:互联网 发布:手机变号码软件 编辑:程序博客网 时间:2024/05/16 14:31
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 (<=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
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.


IDEA

1.用到string中查找子串的函数string s; s.find(substr); 不存在返回-1;存在返回子串的起始位置

2.getline(cin,s);使用时注意前面有scanf或cin的情况,应加cin.get(),吃的回车或空格

3.PAT系统出现内部错误,好忧伤。。。


CODE

#include<iostream>#include<cstring>#include<algorithm>#include<vector>#include<fstream>using namespace std;struct Book{string id;string title;string author;string keywords;string publisher;string year;};int cmp(Book b1,Book b2){return b1.id<b2.id;}int search(int num,string str,vector<Book> &vec){int count=0;vector<Book>::iterator it;switch(num){case 1:for(it=vec.begin();it!=vec.end();it++){if(str==(*it).title){count++;cout<<(*it).id<<endl;}}break;case 2:for(it=vec.begin();it!=vec.end();it++){if(str==(*it).author){count++;cout<<(*it).id<<endl;}}break;case 3:for(it=vec.begin();it!=vec.end();it++){if(((*it).keywords).find(str)!=-1){count++;cout<<(*it).id<<endl;}}break;case 4:for(it=vec.begin();it!=vec.end();it++){if(str==(*it).publisher){count++;cout<<(*it).id<<endl;}}break;case 5:for(it=vec.begin();it!=vec.end();it++){if(str==(*it).year){count++;cout<<(*it).id<<endl;}}break;}return count;}int main(){//freopen("input.txt","r",stdin);int n,m;cin>>n;vector<Book> vec;cin.get();for(int i=0;i<n;i++){Book book;getline(cin,book.id);getline(cin,book.title);getline(cin,book.author);getline(cin,book.keywords);getline(cin,book.publisher);getline(cin,book.year);//cout<<book.id<<endl<<book.title<<endl<<book.author<<endl<<book.keywords<<endl<<book.publisher<<endl<<book.year<<endl; vec.push_back(book);}//cout<<endl;sort(vec.begin(),vec.end(),cmp);//for(int i=0;i<n;i++){//cout<<vec[i].id<<endl<<vec[i].title<<endl<<vec[i].author<<endl<<vec[i].keywords<<endl<<vec[i].publisher<<endl<<vec[i].year<<endl;//}//cout<<endl;cin>>m;int num;string str;for(int i=0;i<m;i++){scanf("%d:",&num);cin.get();//吃掉空格getline(cin,str);cout<<num<<": "<<str<<endl;int count;count=search(num,str,vec);if(count==0){cout<<"Not Found"<<endl;}}//fclose(stdin);return 0;}




0 0
原创粉丝点击