fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <locale>
  5. #include <sstream>
  6. using namespace std;
  7.  
  8. struct colon_is_space : std::ctype<char> {
  9. colon_is_space() : std::ctype<char>(get_table()) {}
  10. static mask const* get_table()
  11. {
  12. static mask rc[table_size];
  13. rc[','] = std::ctype_base::space;
  14. rc['['] = std::ctype_base::space;
  15. rc[']'] = std::ctype_base::space;
  16. return &rc[0];
  17. }
  18. };
  19.  
  20. void printList(const vector<int>& numbers,const bool& reversed,const int& startList,const int& endList)
  21. {
  22. cout << "[";
  23. bool second = false;
  24.  
  25. if(reversed)
  26. {
  27. for (int i=endList-1; i>=startList;--i)
  28. {
  29. if(second) cout << ",";
  30. cout << numbers[i];
  31. second = true;
  32. }
  33. }
  34. else
  35. {
  36. for (int i=startList; i<endList;++i)
  37. {
  38. if(second) cout << ",";
  39. cout << numbers[i];
  40. second = true;
  41. }
  42. }
  43. cout << "]" << "\n";
  44. }
  45.  
  46. void swapBool(bool& someBool)
  47. {
  48. someBool = !someBool;
  49. }
  50.  
  51. bool deleteElem(const bool& reversed,int& startList,int& endList)
  52. {
  53. bool deleted;
  54. if(reversed) --endList;
  55. else ++startList;
  56. (endList>=startList)? deleted=true : deleted=false;
  57. return deleted;
  58. }
  59.  
  60. int main()
  61. {
  62. using std::string;
  63. using std::cin;
  64. using std::locale;
  65. ios::sync_with_stdio(false);
  66. cin.tie(NULL);
  67.  
  68. int testcases;
  69. cin >> testcases;
  70.  
  71. vector<int> numbers;
  72. for (int i=0;i<testcases;++i)
  73. {
  74. string command;
  75. int sizeOfList;
  76. cin >> command >> sizeOfList;
  77. cout << "command "<< command << " sizeOfList "<< sizeOfList << endl;
  78. string line;
  79. cin>>line;
  80. bool reversed = false;
  81. bool shouldPrint = true;
  82. int startList=0;
  83. int endList=sizeOfList;
  84. for(int j=0;j<command.size();++j)
  85. {
  86. if(command.at(j) == 'R') swapBool(reversed);
  87. else
  88. {
  89. if (deleteElem(reversed,startList,endList) == false)
  90. {
  91. cout << "error\n";
  92. shouldPrint = false;
  93. break;
  94. }
  95. }
  96. }
  97. if (shouldPrint)
  98. {
  99. stringstream ss(line);
  100. ss.imbue(locale(ss.getloc(), new colon_is_space));
  101. int singleNumber;
  102. for(int j=0;j<sizeOfList;++j)
  103. {
  104. ss >> singleNumber;
  105. numbers[j]=singleNumber;
  106. }
  107. printList(numbers,reversed,startList,endList);
  108. }
  109. numbers.clear();
  110. }
  111. return 0;
  112. }
Runtime error #stdin #stdout 0s 15248KB
stdin
4
RDD
4
[1,2,3,4]
DD
1
[42]
RRD
6
[1,1,2,3,5,8]
D
0
[]
stdout
command RDD sizeOfList 4