分析
这是一道map+vector的题目,定义一个string映射到vector即可,唯一的麻烦点就是在输入的时候要处理关键字!
ac代码
#include<bits/stdc++.h>
using namespace std;
map< string,vector<string> > m;
int main(){
int n;
cin>>n;
getchar();
for(int i=0;i<n;i++){
string ID,name,author,keyword,printer,year;
getline(cin,ID);
getline(cin,name);
if(m.count(name)==0){
vector<string> v;
v.push_back(ID);
m[name]=v;
}else{
m[name].push_back(ID);
}
getline(cin,author);
if(m.count(author)==0){
vector<string> v;
v.push_back(ID);
m[author]=v;
}else{
m[author].push_back(ID);
}
getline(cin,keyword);
int index=0;
while(index<keyword.length()){
string word="";
while(keyword[index]!=' ' && index<keyword.length()){
word+=keyword[index];
index++;
}
index++;
if(m.count(word)==0){
vector<string> v;
v.push_back(ID);
m[word]=v;
}else{
m[word].push_back(ID);
}
}
if(m.count(keyword)==0){
vector<string> v;
v.push_back(ID);
m[keyword]=v;
}else{
m[keyword].push_back(ID);
}
getline(cin,printer);
if(m.count(printer)==0){
vector<string> v;
v.push_back(ID);
m[printer]=v;
}else{
m[printer].push_back(ID);
}
getline(cin,year);
if(m.count(year)==0){
vector<string> v;
v.push_back(ID);
m[year]=v;
}else{
m[year].push_back(ID);
}
}
//cout<<m["The Testing Book"][1]<<endl;
int p;
cin>>p;
getchar();
for(int i=0;i<p;i++){
string s;
getline(cin,s);
string str=string(s,3,s.length());
cout<<s<<endl;
if(m.count(str)==1){
sort(m[str].begin(),m[str].end());
for(int j=0;j<m[str].size();j++){
cout<<m[str][j]<<endl;
}
}else{
cout<<"Not Found"<<endl;
}
}
}