fork download
  1. //http://c...content-available-to-author-only...e.cn/stlpractice2015/A/
  2. /*
  3.  描述
  4.  
  5.   写一个程序完成以下命令:
  6.   new id ——新建一个指定编号为id的序列(id<10000)
  7.   add id num——向编号为id的序列加入整数num
  8.   merge id1 id2——合并序列id1和id2中的数,并将id2清空
  9.   unique id——去掉序列id中重复的元素
  10.   out id ——从小到大输出编号为id的序列中的元素,以空格隔开
  11. 输入
  12.   第一行一个数n,表示有多少个命令( n<=200000)。以后n行每行一个命令。
  13. 输出
  14.   按题目要求输出。
  15.  */
  16. #include <iostream>
  17. using namespace std;
  18. #include <cstdio>
  19. #include <string>
  20. #include <vector>
  21. #include <list>
  22.  
  23. const int MAX = 255;
  24. void getPara(const string &str,int para[3]);
  25. size_t getIndex(vector<int> &list_index,int &var);
  26. void outputElements(list<int> &list0);
  27. int main(void)
  28. {
  29. int n;
  30. int index;
  31. string func;
  32. vector<list<int> > list_vec;
  33. vector<int> list_index;
  34. (cin >> n).get();
  35. for(index = 0;index < n;index++)
  36. {
  37. int para[3] = {0};
  38. func.clear();
  39. getline(cin,func);
  40. //new 1
  41. if(func.find("new")!= string::npos)
  42. {
  43. getPara(func,para);
  44. list<int> new_list;
  45. list_vec.push_back(new_list);
  46. list_index.push_back(para[0]);
  47. continue;
  48. }
  49. //out 1
  50. if(func.find("out")!= string::npos)
  51. {
  52. getPara(func,para);
  53. list<int> temp_list(list_vec[getIndex(list_index,para[0])]);
  54. if(!temp_list.size())
  55. {
  56. cout << endl;
  57. continue;
  58. }
  59. else
  60. outputElements(temp_list);
  61. continue;
  62. }
  63. //add 1 1
  64. if(func.find("add")!= string::npos)
  65. {
  66. getPara(func,para);
  67. list_vec[getIndex(list_index,para[0])].push_back(para[1]);
  68. continue;
  69. }
  70. //merge 1 2
  71. //merge(list &list0):will remove all elements in list0
  72. //both containers shall already be ordered
  73. if(func.find("merge")!= string::npos)
  74. {
  75. size_t j = 0;
  76. size_t list_id[3] = {0};
  77. getPara(func,para);
  78. for(;j < list_index.size();j++)
  79. {
  80. if(list_index[j] == para[0])
  81. list_id[0] = j;
  82. if(list_index[j] == para[1])
  83. list_id[1] = j;
  84. }
  85. list_vec[list_id[0]].sort();
  86. list_vec[list_id[1]].sort();
  87. list_vec[list_id[0]].merge(list_vec[list_id[1]]);
  88. continue;
  89. }
  90. //unique 1
  91. if(func.find("unique")!= string::npos)
  92. {
  93. getPara(func,para);
  94. list_vec[getIndex(list_index,para[0])].unique();
  95. continue;
  96. }
  97. }
  98. return 0;
  99. }
  100. void getPara(const string &str,int para[])
  101. {
  102. int i = 0;
  103. int num = 0;//Indicate the number of parameters
  104. char buf[MAX];
  105. while ( str.c_str()[i] != '\0' )
  106. {
  107. if (isdigit(str[i]))
  108. {
  109. buf[i] = str[i];
  110. num++;
  111. }
  112. else buf[i] = ' ';
  113. ++i;
  114. }
  115. buf[i] = '\0';
  116. if(num == 2)//Two parameters,such as:add 1 1
  117. sscanf(buf,"%d %d", &para[0], &para[1]);
  118. else
  119. sscanf(buf,"%d",&para[0]);
  120. }
  121. size_t getIndex(vector<int> &list_index,int &var)
  122. {
  123. size_t j = 0;
  124. for(;j < list_index.size();j++)
  125. {
  126. if(list_index[j] == var)
  127. return j;
  128. }
  129. return -1;
  130. }
  131. void outputElements(list<int> &list0)
  132. {
  133. list<int>::iterator it;
  134. for(it = list0.begin();it != list0.end();it++)
  135. {
  136. cout << *it << " ";
  137. }
  138. cout << endl;
  139. }
Success #stdin #stdout 0s 3284KB
stdin
16
new 1
new 2
add 1 1
add 1 2
add 1 3
add 2 1
add 2 2
add 2 3
add 2 4
out 1
out 2
merge 1 2
out 1
out 2
unique 1
out 1
stdout
1 2 3 
1 2 3 4 
1 1 2 2 3 3 4 

1 2 3 4