1022. Digital Library (30)

来源:互联网 发布:java管理系统界面 编辑:程序博客网 时间:2024/05/23 20:04

题目:https://www.patest.cn/contests/pat-a-practise/1022

注意:map<string, set<int> >的使用,调用函数时使用引用加快运行速度,不然超时


#include<string>#include<set>#include<map>#include<iostream>#include<cstdio>using namespace std;int n,m,id,type;string temp;char str[100];map<string, set<int> > title,author,key,publish,year;void qury(string &temp,map<string, set<int> > &mp){if(mp.find(temp) == mp.end()){printf("Not Found\n");}else{for(set<int>::iterator it = mp[temp].begin(); it!=mp[temp].end(); it++)printf("%07d\n",*it);}}int main(){cin>>n;for(int i=0; i<n; i++){//要用getline获取一行!!!scanf("%d",&id);getchar();//titlegetline(cin,temp);title[temp].insert(id);//authorgetline(cin,temp);author[temp].insert(id);//keywhile (cin>>temp){key[temp].insert(id);char x;x = getchar();if(x == '\n')break;}//publishgetline(cin,temp);publish[temp].insert(id);//yeargetline(cin,temp);year[temp].insert(id);}cin>>m;for(int i=0; i<m; i++){//不要太死板,getline可以和scanf混用//getline前面的参数是cinscanf("%d: ",&type);getline(cin,temp);cout<<type<<": "<<temp<<endl;if(type == 1) qury(temp, title);else if(type==2) qury(temp,author);else if(type==3) qury(temp,key);else if(type==4) qury(temp,publish);else if(type==5) qury(temp,year);}return 0;}


0 0
原创粉丝点击