当前位置: 代码迷 >> 综合 >> PAT甲级-1022 Digital Library (30分)
  详细解决方案

PAT甲级-1022 Digital Library (30分)

热度:78   发布时间:2023-09-26 23:50:01.0

点击链接PAT甲级-AC全解汇总

题目:
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 haracters without any white space, and the keywords are separated by exactly one pace;
  • 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

题意:
这道题不难,输入N本书,包括六个数据【id,标题,作者,关键词,出版社,出版年份】,然后对于后五个内容,查找相关的id。
只要熟练运用map和set,难点就只有把关键词区分开;
注意: 一开始后两个case报错,发现是id的的问题,题目要求都是7位的,所以前面要补0补齐7位。

我的代码:

#include<bits/stdc++.h>
using namespace std;bool flag_none=true;void my_print_set(set<int> my_set)
{
    if(my_set.size())flag_none=false;for(auto it:my_set)printf("%07d\n",it);
}int main()
{
    map<string,set<int> >title_to_ids;map<string,set<int> >author_to_ids;map<string,set<int> >key_to_ids;map<string,set<int> >publisher_to_ids;map<string,set<int> >year_to_ids;int N;//输入cin>>N;for(int i=0;i<N;i++){
    //输入一本书的六个信息int id;string title,author,keys,publisher,year;cin>>id;getchar();getline(cin,title);getline(cin,author);getline(cin,keys);getline(cin,publisher);getline(cin,year);//存储五个信息到对应的集合,其中keys要分开存title_to_ids[title].insert(id);author_to_ids[author].insert(id);publisher_to_ids[publisher].insert(id);year_to_ids[year].insert(id);int pos=keys.find(" ");while(pos!=-1){
    key_to_ids[keys.substr(0,pos)].insert(id);keys.erase(0,pos+1);pos=keys.find(" ");}key_to_ids[keys].insert(id);}//查询cin>>N;for(int i=0;i<N;i++){
    int t;cin>>t;getchar();//':'getchar();//' 'string str;getline(cin,str);cout<<t<<": "<<str<<endl;switch(t){
    case 1://1.title{
    my_print_set(title_to_ids[str]);break;}case 2://2.author{
    my_print_set(author_to_ids[str]);break;}case 3://3.key{
    my_print_set(key_to_ids[str]);break;}case 4://4.publisher{
    my_print_set(publisher_to_ids[str]);break;}case 5://5.year{
    my_print_set(year_to_ids[str]);break;}}if(flag_none)cout<<"Not Found"<<endl;flag_none=true;}return 0;
}
  相关解决方案