1022 Digital Library (30 分)(******)

it2022-05-05  220

#include <iostream> #include <cstdio> #include <map> #include <algorithm> #include <set> #include <string> #include <cstring> using namespace std; map<string,set<int> >mptitle,mpauthor,mpkey,mpprint,mpyear; void getId(map<string,set<int> >&mp,string &query)//引用来减少时间 { set<int>::iterator it; if(mp.find(query) == mp.end()) cout<<"Not Found"<<endl; else { for(it = mp[query].begin();it!=mp[query].end();it++) { printf("d\n",*it);//id必须是七位 } } } int main() { int n; scanf("%d",&n); for(int i = 0;i<n;i++) { string temp; int id; scanf("%d",&id); char c = getchar(); getline(cin,temp); mptitle[temp].insert(id); // cout<<*mptitle[temp].begin()<<endl; getline(cin,temp); mpauthor[temp].insert(id); // cout<<*mpauthor[temp].begin()<<endl; // getline(cin,temp); // mpkey[temp].insert(id); while(cin>>temp) { mpkey[temp].insert(id); // cout<<*mpkey[temp].begin()<<endl; c=getchar(); if(c == '\n') break; } getline(cin,temp); mpprint[temp].insert(id); getline(cin,temp); mpyear[temp].insert(id); } int m; scanf("%d",&m); for(int i = 0;i<m;i++) { int type; string query; scanf("%d: ",&type); // char c = getchar(); getline(cin,query); printf("%d: ",type); cout<<query<<endl; if(type == 1) getId(mptitle,query); else if(type == 2) getId(mpauthor,query); else if(type == 3) getId(mpkey,query); else if(type == 4) getId(mpprint,query); else getId(mpyear,query); } // set<int>::iterator it = mpyear["2011"].begin(); // cout<<*it<<endl; return 0; }

第一次的答案,问题在于忽略了问题的前缀。

#include <iostream> #include <cstdio> #include <map> #include <algorithm> #include <sstream> #include <string> #include <cstring> using namespace std; int main() { int n; scanf("%d",&n); map<string,string>mp[10010]; for(int i = 0;i<n;i++) { string s; cin>>s; scanf("%*c"); for(int j = 0;j<5;j++) { string temp; getline(cin,temp); if(j==2) { string b; stringstream ss(temp); while(ss>>b) { // cout<<b<<endl; mp[i][b] = s; } } else mp[i][temp] = s; // cout<<mp[j][s]<<endl; } } int m; scanf("%d%*c",&m); for(int i = 0;i<m;i++) { string query; getline(cin,query); cout<<query<<endl; query.erase(query.begin(),query.begin() + 3); int index = 0; string ans[10010]; for(int j = 0;j<n;j++) { map<string,string>::iterator it = mp[j].find(query); if(it != mp[j].end()) { ans[index] = mp[j][query]; // cout<<"###"<<mp[j][query]<<endl; // cout<<"###"<<ans[index]<<endl; index++; } } sort(ans,ans+index); if(index == 0) cout<<"Not Found"; else { for(int a = 0;a<index;a++) cout<<ans[a]<<endl; } } return 0; }

最新回复(0)