题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336
这题应该算是最复杂的容器题之一了,用的是map
感觉不太会出这种题,对着算法笔记打了一遍代码理解了一下
注意一下第48、49行
代码
#include<algorithm>#include<iostream>#include<vector>#include<cstdio>#include<map>#include<set>#include<string>using namespace std;map<string, set<int> > mpTitle, mpAuthor, myKey, mpPub, mpYear;void query(map<string, set<int> > &mp, string &str){if(mp.find(str) == mp.end()){//没找到关键词printf("Not Found\n");} else {for(auto it = mp[str].begin(); it != mp[str].end(); it++){printf("%07d\n", *it);}}}int main(){int n, m, type;string title, author, key, pub, year;scanf("%d", &n);for(int i = 0; i < n; i++){int tempid;scanf("%d", &tempid);char c = getchar();//吸收换行符getline(cin, title);mpTitle[title].insert(tempid);getline(cin, author);mpAuthor[author].insert(tempid);while(cin >> key){myKey[key].insert(tempid);c = getchar();if(c == '\n') break;}getline(cin, pub);mpPub[pub].insert(tempid);getline(cin, year);mpYear[year].insert(tempid);}string temp;scanf("%d",&m);for(int i = 0; i < m; i++){scanf("%d: ",&type);getline(cin, temp);cout<<type<<": "<<temp<<endl;if(type == 1) query(mpTitle, temp);else if(type == 2) query(mpAuthor, temp);else if(type == 3) query(myKey, temp);else if(type == 4) query(mpPub,temp);else if(type ==5) query(mpYear, temp);}return 0;}
