问题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336
题意:给出N本书的编号,书名,作者,关键词(多个),出版社,出版年份;并给出M个查询,每个查询给出书名,作者,关键词(单个),出版社及出版年份中的一个,要求输出满足该给出信息的所有书的编号。
Note:
1.map<string, set<int>>
把字符串string映射到一个容器set<int>,即把一个string映射到一个元素均为整数且有序的集合
2 特别注意本题中的关键词提取,用cin读入单个关键词,然后用getchar()接收在这个关键词后面的字符;若是换行符则结束关键词输入,若是空格则继续读入关键字。
3 getline(cin, str)整行读入
4 scanf("%d: ");读入查询类型及其后的,冒号,空格。
#include<cstdio>
#include<iostream>
#include<map>
#include<set>
using namespace std;
void query(map<string, set<int> > &mp, string &temp){
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(){
int n, m, id;
map<string, set<int> > mpTitle, mpAuthor, mpKey, mpPub, mpYear;
string Title, Author, Key, Pub, Year;
cin >> n;
char c;
while(n--){
cin >> id;
c = getchar();
getline(cin, Title);
mpTitle[Title].insert(id);
getline(cin, Author);
mpAuthor[Author].insert(id);
while(cin >> Key){
mpKey[Key].insert(id);
c = getchar();
if(c == '\n')
break;
}
getline(cin, Pub);
mpPub[Pub].insert(id);
getline(cin, Year);
mpYear[Year].insert(id);
}
string temp;
int k;
cin >> m;
while(m--){
scanf("%d: ", &k);
getline(cin, temp);
cout << k <<": "<<temp<<endl;
if(k == 1)
query(mpTitle, temp);
else if(k == 2)
query(mpAuthor, temp);
else if(k == 3)
query(mpKey, temp);
else if(k == 4)
query(mpPub, temp);
else
query(mpYear, temp);
}
return 0;
}