题目描述
ACM俱乐部可以为每一个学校提供一个属于自己的OJ(Online Judge,在线判题系统),假如你现在已经成为了自己学校的OJ管理员,你自然就拥有了两项基本权限:添加题目到OJ、删除OJ上已添加的题目。从此以后,要添要删你做主!
题目的添加规则如下:
题目序号从1000开始,每个题目的序号具有惟一性,且以1为单位逐渐增大。
如果某道题目被删除,则此题目序号作废,后面新添加的题目不会再次占用此序号。
现在给你若干添加删除操作,请你按照题目序号从小到大输出OJ中最终保留的题目信息。
题目的添加规则如下:
题目序号从1000开始,每个题目的序号具有惟一性,且以1为单位逐渐增大。
如果某道题目被删除,则此题目序号作废,后面新添加的题目不会再次占用此序号。
现在给你若干添加删除操作,请你按照题目序号从小到大输出OJ中最终保留的题目信息。
输入
输入的第一行为一个正整数T(0<T<=10),表示有T组输入。
每组输入包含若干行,每行输入有三种情况:
(1)当如入“Add”,表示添加题目,然后空一格输入要添加的题目名称,题目名称的长度小于10。
(2)当输入“Delete”,表示删除题目,然后空一格输入要删除的题目名称。
(3)当输入“End”,表示本组输入结束,此行不作处理。
测试数据保证每组的第一行输入一定是Add操作,同时,删除的一定是当前OJ中存在的题目,题目名称只包含英文字母,且题目名称没有重名。
每组输入包含若干行,每行输入有三种情况:
(1)当如入“Add”,表示添加题目,然后空一格输入要添加的题目名称,题目名称的长度小于10。
(2)当输入“Delete”,表示删除题目,然后空一格输入要删除的题目名称。
(3)当输入“End”,表示本组输入结束,此行不作处理。
测试数据保证每组的第一行输入一定是Add操作,同时,删除的一定是当前OJ中存在的题目,题目名称只包含英文字母,且题目名称没有重名。
输出
对于每组输入,输出两行,第一行为最终OJ中保留的题目序号,从小到大排列,题目序号之间空一格。
第二行为上一行对应题目序号的题目名称,题目名称之间空一格。
如果最终OJ中没有保留的题目,则输出两个空行。
第二行为上一行对应题目序号的题目名称,题目名称之间空一格。
如果最终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 }