#include<iostream> #include<vector> #include<map> #include<sstream> #include<algorithm> using namespace std; map<string ,vector<string> >title,keywords,author,publisher,publisher_year; int n; bool cmp(string a,string b){ return a<b; } void res(map<string ,vector<string> > &m,string &str){ if(m[str].size() == 0)cout<<"Not Found "; else{ sort(m[str].begin(),m[str].end(),cmp); for(auto i:m[str]){ printf("%s ",i.c_str()); } } } int main() { cin>>n; // getchar(); for(int i=1;i<=n;i++){ string id; getline(cin,id); string s; getline(cin,s); title[s].push_back(id); // getchar(); getline(cin,s); author[s].push_back(id); // getchar(); while(cin>>s){ keywords[s].push_back(id); char c = getchar(); if(c == ' ')break; } // getchar(); getline(cin,s); publisher[s].push_back(id); // getchar(); getline(cin,s); publisher_year[s].push_back(id); // getchar(); } int m; cin>>m; string s; int tmp ; for(int i=1;i<=m;i++){ scanf("%d: ",&tmp); getline(cin,s); printf("%d: %s ",tmp,s.c_str()); if(tmp == 1)res(title,s) ; else if(tmp == 2) res(author ,s); else if(tmp == 3) res(keywords,s); else if(tmp == 4) res(publisher,s); else if(tmp == 5) res(publisher_year,s); } return 0; }
scanf(),cin在读入一个数据时会把末尾的回车,换行留在缓冲区,如果后面紧接着读入字符串,需用space std;
map<string ,vector<string> >title,keywords,author,publisher,publisher_year;
int n;
bool cmp(string a,string b){
return a<b;
}
void res(map<string ,vector<string> > &m,string &str){
if(m[str].size() == 0)cout<<"Not Found ";
else{
sort(m[str].begin(),m[str].end(),cmp);
for(auto i:m[str]){
printf("%s ",i.c_str());
}
}
}
int main()
{
cin>>n;
// getchar();
for(int i=1;i<=n;i++){
string id;
getline(cin,id);
string s;
getline(cin,s);
title[s].push_back(id);
// getchar();
getline(cin,s);
author[s].push_back(id);
// getchar();
while(cin>>s){
keywords[s].push_back(id);
char c = getchar();
if(c == ' ')break;
}
// getchar();
getline(cin,s);
publisher[s].push_back(id);
// getchar();
getline(cin,s);
publisher_year[s].push_back(id);
// getchar();
}
int m;
cin>>m;
string s;
int tmp ;
for(int i=1;i<=m;i++){
scanf("%d: ",&tmp);
getline(cin,s);
printf("%d: %s ",tmp,s.c_str());
if(tmp == 1)res(title,s) ;
else if(tmp == 2) res(author ,s);
else if(tmp == 3) res(keywords,s);
else if(tmp == 4) res(publisher,s);
else if(tmp == 5) res(publisher_year,s);
}
return 0;
}
scanf(),cin在读入一个数据时会把末尾的回车,换行留在缓冲区,如果后面紧接着读入字符串,需用先用getchar()函数吞掉那个回车符,再用getline函数多次读入字符串,getline()会将末尾换行符丢弃