题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336
题目大意:图书馆存书,书有ID、标题、作者、关键字、发行商、发行年月。给你一些读者的询问,请你找出对应书的ID,升序输出。
思路:一对多映射,用map。
#include<bits/stdc++.h>
using namespace std;
const int maxn=10000+50;
int n,m,t1,t2,t3,t4,t5,pos;
string id,tt,at,ky,ps,y,req;
map<string,int> mp1,mp2,mp3,mp4,mp5;
vector<string> title[maxn],author[maxn],keyword[maxn],pubs[maxn],year[maxn];
int main()
{
cin>>n;
getchar();
t1=t2=t3=t4=t5=1;
for(int i=1;i<=n;i++){
getline(cin,id);
getline(cin,tt);
if(!mp1[tt]) mp1[tt]=t1++;
pos=mp1[tt];
title[pos].push_back(id);
getline(cin,at);
if(!mp2[at]) mp2[at]=t2++;
pos=mp2[at];
author[pos].push_back(id);
getline(cin,ky);
string tmp="";
for(int i=0;i<ky.length();i++){
if(ky[i]!=' ') tmp+=ky[i];
if(ky[i]==' '||i==ky.length()-1){
if(!mp3[tmp]) mp3[tmp]=t3++;
pos=mp3[tmp];
keyword[pos].push_back(id);
tmp="";
}
}
getline(cin,ps);
if(!mp4[ps]) mp4[ps]=t4++;
pos=mp4[ps];
pubs[pos].push_back(id);
getline(cin,y);
if(!mp5[y]) mp5[y]=t5++;
pos=mp5[y];
year[pos].push_back(id);
}
cin>>m;
getchar();
for(int i=1;i<=m;i++){
getline(cin,req);
cout<<req<<endl;
if(req[0]=='1'){
tt=req.substr(3);
pos=mp1[tt];
if(!mp1[tt]) cout<<"Not Found"<<endl;
else {
sort(title[pos].begin(),title[pos].end());
for(int i=0;i<title[pos].size();i++){
cout<<title[pos][i]<<endl;
}
}
}
else if(req[0]=='2'){
at=req.substr(3);
pos=mp2[at];
if(!mp2[at]) cout<<"Not Found"<<endl;
else {
sort(author[pos].begin(),author[pos].end());
for(int i=0;i<author[pos].size();i++){
cout<<author[pos][i]<<endl;
}
}
}
else if(req[0]=='3'){
ky=req.substr(3);
pos=mp3[ky];
if(!mp3[ky]) cout<<"Not Found"<<endl;
else {
sort(keyword[pos].begin(),keyword[pos].end());
for(int i=0;i<keyword[pos].size();i++){
cout<<keyword[pos][i]<<endl;
}
}
}
else if(req[0]=='4'){
ps=req.substr(3);
pos=mp4[ps];
if(!mp4[ps]) cout<<"Not Found"<<endl;
else {
sort(pubs[pos].begin(),pubs[pos].end());
for(int i=0;i<pubs[pos].size();i++){
cout<<pubs[pos][i]<<endl;
}
}
}
else if(req[0]=='5'){
y=req.substr(3);
pos=mp5[y];
if(!mp5[y]) cout<<"Not Found"<<endl;
else {
sort(year[pos].begin(),year[pos].end());
for(int i=0;i<year[pos].size();i++){
cout<<year[pos][i]<<endl;
}
}
}
}
}