PAT (Advanced Level) Practice 1022 Digital Library (30 point(s))

题目 PAT A 1022

链接

题目大意

n本书,每本书输入的格式为

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].

然后由m个询问,询问的时候,根据上面的某个变量,查询出书的id,排序输出。
询问格式为

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

解题思路

  • 本题只需对每一个变量设置一个map<string,vector<string>>即可。
  • 通过本题,应该学到map的使用
  • 学会一些读入操作getline,stringstream

如何读入一行,并且按照空格分割

string str,word;
getline(str);
stringstream line(str);
while(line>>word){
	cout<<word<<endl;
}
#include<bits/stdc++.h>
using namespace std;
map<string,vector<string>>Title,Author,Keyword,Puplisher,Year;
int main(){
    int n,m;
    string tmp,keyword,id,author,title,publisher,year;
    while(getline(cin,tmp)){
        n=stoi(tmp);
        for(int i=1;i<=n;++i){
            getline(cin,id);
            getline(cin,title);
            Title[title].push_back(id);
            getline(cin,author);
            Author[author].push_back(id);
            getline(cin,tmp);
            stringstream line(tmp);
            while(line>>keyword){
                Keyword[keyword].push_back(id);
            }
            getline(cin,publisher);
            Puplisher[publisher].push_back(id);
            getline(cin,year);
            Year[year].push_back(id);
        }
        getline(cin,tmp);
        m=stoi(tmp);
        while(m--){
            string query;
            getline(cin,query);
            cout<<query<<endl;
            int type=query[0]-'0';
            query=query.substr(3);
            switch (type){
            case 1:
                if(Title[query].size()==0){
                    cout<<"Not Found"<<endl;
                    break;
                }
                sort(Title[query].begin(),Title[query].end());
                for(string s:Title[query]) cout<<s<<endl;
                break;
            case 2:
                if(Author[query].size()==0){
                    cout<<"Not Found"<<endl;
                    break;
                }
                sort(Author[query].begin(),Author[query].end());
                for(string s:Author[query]) cout<<s<<endl;
                break;
            case 3:
                if(Keyword[query].size()==0){
                    cout<<"Not Found"<<endl;
                    break;
                }
                sort(Keyword[query].begin(),Keyword[query].end());
                for(string s:Keyword[query]) cout<<s<<endl;
                break;
            case 4:
                if(Puplisher[query].size()==0){
                    cout<<"Not Found"<<endl;
                    break;
                }
                sort(Puplisher[query].begin(),Puplisher[query].end());
                for(string s:Puplisher[query]) cout<<s<<endl;
                break;
            case 5:
                if(Year[query].size()==0){
                    cout<<"Not Found"<<endl;
                    break;
                }
                sort(Year[query].begin(),Year[query].end());
                for(string s:Year[query]) cout<<s<<endl;
                break;
            }
        }

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值