题目注意点
- 由于每个关键词string都可能对应多本书,不能用string对int的唯一映射,应该用string对set《int》的映射
- 注意关键词需要一个个词分开读取
- 输出书号时需要按照7位数字输出,最后两个点就是这个问题
题解
注释部分也可以读取关键词,类似于PAT A1071 Speech Patterns (25 分)的解法
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;
map<string, set<int> > name;
map<string, set<int> > author;
map<string, set<int> > keyword;
map<string, set<int> > publisher;
map<string, set<int> > year;
void printSet(set<int> a) {
if (a.size() == 0) {
printf("Not Found\n");
return;
}
for (set<int>::iterator it = a.begin(); it != a.end(); it++) {
printf("%07d\n", *it);
}
}
int main() {
int n, m;
scanf("%d", &n);
getchar();
for (int i = 0; i < n; i++) {
string str;
int id;
scanf("%d", &id);
getchar();
getline(cin, str);
name[str].insert(id);
getline(cin, str);
author[str].insert(id);
/*getline(cin, str);
int j = 0;
while (j < str.size()) {
string word;
while (j < str.size() && str[j]!=' ') {
word += str[j];
j++;
}
keyword[word].insert(id);
if (j < str.size() && str[j] == ' ') j++;
}*/
while (cin >> str) {
keyword[str].insert(id);
char c = getchar();
if (c == '\n') break;
}
getline(cin, str);
publisher[str].insert(id);
getline(cin, str);
year[str].insert(id);
}
scanf("%d", &m);
getchar();
for (int i = 0; i < m; i++) {
string str;
getline(cin, str);
int op = str[0]-'0';
cout << str << endl;
str.erase(0, 3);
if (op == 1) printSet(name[str]);
if (op == 2) printSet(author[str]);
if (op == 3) printSet(keyword[str]);
if (op == 4) printSet(publisher[str]);
if (op == 5) printSet(year[str]);
}
return 0;
}