题目链接:
https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336
参考代码:
#include <iostream>
#include <map>
#include <set>
using namespace std;
map<string, set<int> > title, author, key, pub, year;//构造从书名,作者,关键字,出版社,出版年份到书籍ID的映射
void query(map<string, set<int> > &m, string &str)
{
if(m.find(str) != m.end()) //多个查找结果
{
for(auto it = m[str].begin(); it != m[str].end(); it++)
printf("%07d\n", *it);
} else
cout << "Not Found\n";
}
int main() {
int n, m, id, num;
scanf("%d", &n);
string ttitle, tauthor, tkey, tpub, tyear;
for(int i = 0; i < n; i++) {
scanf("%d\n", &id);
getline(cin, ttitle);// 书名中可能含有空格,所以要用getline
title[ttitle].insert(id);// 插入到映射中 映射键值id
getline(cin, tauthor);
author[tauthor].insert(id);
while(cin >> tkey) { // cin不接受换行符,可以作为输入的终点
key[tkey].insert(id);
char c = getchar();
if(c == '\n') break;
}
getline(cin, tpub);
pub[tpub].insert(id);
getline(cin, tyear);
year[tyear].insert(id);
}
scanf("%d", &m);
for(int i = 0; i < m; i++) {
scanf("%d: ", &num);
string temp;
getline(cin, temp);
cout << num << ": " << temp << "\n";
if(num == 1) query(title, temp);
else if(num == 2) query(author, temp);
else if(num == 3) query(key, temp);
else if(num == 4) query(pub,temp);
else if(num ==5) query(year, temp);
}
return 0;
}