[关闭]
@xunuo 2017-01-18T12:49:36.000000Z 字数 2683 阅读 869

SB_cyh and his BST one----(set)

Time Limit: 10000 MS Memory Limit: 65536 KB

来源:
swustoj :swustoj 2340

STL


Description

有一个序列含有一定数量的元素,现在要求写一个程序,满足以下几个要求:
【A】支持插入操作(这个序列不允许有重复元素,即是说,如果待插入的元素已经出现在这个序列中,那么请忽略此操作)
【B】支持删除操作(如果此序列中不包含待删除的这个元素,则忽略此操作,否则删除这个元素)
【C】查找元素x的前继元素(前继元素是指:小于x且与x最接近的元素,当然,如果x已经是序列中的最小元素,则x没有前继元素)
【D】查找元素x的后继元素(后继元素是指:大于x且与x最接近的元素,当然,如果x已经是序列中的最大元素,则x没有后继元素)

Input

多组数据(整个文件以输入 -1 结束)
对于每组数据,有若干行(最多100000行),表示的意义如下:
【A】 insert x  
【B】 delete x
【C】 predecessor x
【D】 successor x
这4种操作的意义与上面的定义相对应!
【E】 print  表示从小到大输出序列中的所有元素
【F】 end 表示结束本组数据

每组输入数据后有一空行!

Output

对于以上6种操作,分别输出对应信息,如下:
【A】 insert x  不用输出任何信息
【B】 delete x  如果x存在,则删除x,否则输出 Input Error
【C】 predecessor x  如果x不存在,输出 Input     Error;否则如果x是序列中的最小元素,输出对应信息(见样例),否则输出x的前继元素
【D】 successor x    如果x不存在,输出 Input Error;否则如果x是序列中的最大元素,输出对应信息(见样例),否则输出x的后继元素
【E】 print  从小到大输出序列中的所有元素,每个元素后加一个逗号,并在最后加上 end of print(见样例)
【F】 end 输出 end of this test

Sample Input

insert 20
insert 5
insert 1
insert 15
insert 9
insert 25
insert 23
insert 30
insert 35
print
successor 15
successor 35
successor 25
successor 26
predecessor 1
predecessor 20
predecessor 23
predecessor 15
predecessor 111
delete 9
delete 15
delete 25
delete 23
delete 20
print
end

insert 1
insert 1
insert 2
insert 2
print
delete 1
print
delete 2
print
end

-1

Sample Output

1,5,9,15,20,23,25,30,35,end of print
The successor of 15 is 20
35 is the maximum
The successor of 25 is 30
Input Error
1 is the minimum
The predecessor of 20 is 15
The predecessor of 23 is 20
The predecessor of 15 is 9
Input Error
1,5,30,35,end of print
end of this test
1,2,end of print
2,end of print
end of print
end of this test

Hint

本题:用STL的set 或者 手写set
another:2268

完整代码:

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #include<set>
  5. #include<algorithm>
  6. using namespace std;
  7. int main()
  8. {
  9. set<int>s;
  10. set<int>::iterator it;
  11. char a[100];
  12. int n;
  13. while(scanf("%s",a))
  14. {
  15. if(a[0]=='-')///当输入为-1时,结束程序
  16. break;
  17. if(a[0]=='i')///当输入insert时在set里插入数据
  18. {
  19. scanf("%d",&n);
  20. s.insert(n);
  21. }
  22. if(a[0]=='p'&&a[1]=='r'&&a[2]=='i')///当输入为print时,输出所存的数,且输出end of print;
  23. {
  24. for(it=s.begin();it!=s.end();it++)///从begin到end;
  25. printf("%d,",*it);
  26. printf("end of print\n");
  27. }
  28. if(a[0]=='d')///当输入为delete时删除该输入的数
  29. {
  30. scanf("%d",&n);
  31. if(s.count(n)==0)///如果在set里面没有这个数
  32. printf("Input Error\n");
  33. else
  34. s.erase(n);///删除!!!
  35. }
  36. if(a[0]=='p'&&a[1]=='r'&&a[2]=='e')///找出前驱
  37. {
  38. scanf("%d",&n);
  39. it=s.find(n);
  40. if(s.count(n)==0)///如果没有这个数
  41. printf("Input Error\n");
  42. else if(it==s.begin())///如果输入的数为最小的那个数
  43. printf("%d is the minimum\n",n);
  44. else
  45. {
  46. it=s.lower_bound(n);///lower_bound()是找出与该数大于或等于的第一个数,这里*it=n;
  47. it--;///it--就是比它小的最大的数
  48. printf("The predecessor of %d is %d\n",n,*it);
  49. }
  50. }
  51. if(a[0]=='s')///找出后继
  52. {
  53. scanf("%d",&n);
  54. it=s.find(n);
  55. if(s.count(n)==0)
  56. printf("Input Error\n");
  57. else
  58. {
  59. it++;///it如果是最大的那个数,那么,it++就是s.end();!!!!
  60. if(it==s.end())
  61. printf("%d is the maximum\n",n);
  62. else
  63. {
  64. it=s.upper_bound(n);///upper_bound()是要找出比它第一大的数,即为它的后继
  65. printf("The successor of %d is %d\n",n,*it);
  66. }
  67. }
  68. }
  69. if(a[0]=='e')///end结束!记得清空
  70. {
  71. printf("end of this test\n");
  72. s.clear();
  73. }
  74. }
  75. return 0;
  76. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注