ACM俱乐部可以为每一个学校提供一个属于自己的OJ(Online Judge,在线判题系统),假如你现在已经成为了自己学校的OJ管理员,你自然就拥有了两项基本权限:添加题目到OJ、删除OJ上已添加的题目。从此以后,要添要删你做主!
题目的添加规则如下:
题目序号从1000开始,每个题目的序号具有惟一性,且以1为单位逐渐增大。
如果某道题目被删除,则此题目序号作废,后面新添加的题目不会再次占用此序号。
现在给你若干添加删除操作,请你按照题目序号从小到大输出OJ中最终保留的题目信息。
2 Add acm Add club End Add I Add am Add very Delete very Add happy End
1000 1001 acm club 1000 1001 1003 I am happy
没过的代码 答案错误
1 #include <cstdio> 2 #include <iostream> 3 #include <map> 4 #include <string> 5 #include <algorithm> 6 7 using namespace std; 8 9 10 int main() 11 { 12 int t; 13 map<string,int> mp; 14 cin>>t; 15 while(t--) 16 { 17 string op,str; 18 cin>>op; 19 int num=1000; 20 while(op!="End") 21 { 22 if(op=="Add") 23 { 24 cin>>str; 25 mp[str]=num++; 26 } 27 else if(op=="Delete") 28 { 29 cin>>str; 30 mp.erase(str); 31 } 32 cin>>op; 33 } 34 if(!mp.size()) cout<<"\n\n"; 35 else 36 { 37 map<string,int>::iterator it; 38 for(it=mp.begin();it!=mp.end();it++) 39 { 40 if(it==mp.begin()) cout<<it->second; 41 else cout<<‘ ‘<<it->second; 42 } 43 cout<<endl; 44 for(it=mp.begin();it!=mp.end();it++) 45 { 46 if(it==mp.begin()) cout<<it->first; 47 else cout<<‘ ‘<<it->first; 48 } 49 cout<<endl; 50 } 51 52 mp.clear(); 53 } 54 return 0; 55 }
睡一觉突然觉悟了
map 如果用string当key值,会自动排序...........
所以应用序号当key值
1 #include <cstdio> 2 #include <iostream> 3 #include <map> 4 #include <string> 5 #include <algorithm> 6 7 using namespace std; 8 9 10 int main() 11 { 12 int t; 13 map<int,string> mp; 14 map<int,string>::iterator it; 15 cin>>t; 16 while(t--) 17 { 18 string op,str; 19 cin>>op; 20 int num=1000; 21 while(op!="End") 22 { 23 if(op=="Add") 24 { 25 cin>>str; 26 mp[num++]=str; 27 } 28 else if(op=="Delete") 29 { 30 cin>>str; 31 for(it=mp.begin();it!=mp.end();it++) 32 { 33 if(str==it->second) break; 34 } 35 mp.erase(it); 36 } 37 cin>>op; 38 } 39 if(!mp.size()) cout<<"\n\n"; 40 else 41 { 42 for(it=mp.begin();it!=mp.end();it++) 43 { 44 if(it==mp.begin()) cout<<it->first; 45 else cout<<‘ ‘<<it->first; 46 } 47 cout<<endl; 48 for(it=mp.begin();it!=mp.end();it++) 49 50 { 51 if(it==mp.begin()) cout<<it->second; 52 else cout<<‘ ‘<<it->second; 53 } 54 cout<<endl; 55 } 56 57 mp.clear(); 58 } 59 return 0; 60 }
原文:https://www.cnblogs.com/jiamian/p/10640203.html