fork download
  1. //백준
  2. #include <iostream>
  3. #include <vector>
  4. //#include <utility> //pair
  5. #include <algorithm> //sort//min//lower_bound //reverse
  6. //#include <unordered_map>
  7. //#include <map>
  8. //#include <sstream>
  9. //#include <set>
  10. //#include <string> //stoi
  11. //#include <cstring> //strlen
  12. //#include <numeric> //gcd //accumulate
  13. //#include <cmath> //sqrt //round //pow
  14. //#include <stack>
  15. //#include <queue>
  16. #include <deque>
  17. //#include <tuple>
  18. #include <cctype> //isdigit
  19.  
  20. using namespace std;
  21.  
  22. // void funcD(deque<int>& arr){
  23.  
  24. // arr.pop_front();
  25. // }
  26. // void fucnR(deque<int>& arr){
  27. // reverse(arr.begin(), arr.end());
  28. // }
  29.  
  30. int main(void){
  31.  
  32. ios::sync_with_stdio(false);
  33. cin.tie(nullptr);
  34.  
  35. int T;// 테스트 케이스
  36.  
  37. cin >> T;
  38.  
  39.  
  40.  
  41. string command; //명령 문자열 ex) RDD
  42.  
  43. int n; //배열에 들어있는 수의 개수
  44.  
  45. deque<int> My_dq;
  46.  
  47. string numstr; //숫자가 들어있는 문자열 //[1,2,43,5]
  48.  
  49. //vector<string> temp; //수를 임시로 저장할 문자열 - (, ]) 이게 나오면 덱에 넣기 - planA
  50. int temp1 = 0; //임시 저장할 숫자 - 플랜b
  51.  
  52. while(T--){
  53. cin >> command;
  54. cin >> n;
  55. cin >> numstr;
  56. bool reversed = false;
  57.  
  58. My_dq.clear();
  59.  
  60. //문자열 덱에 넣은 과정
  61. for(char x: numstr){
  62. if(x == '['){
  63. continue;
  64. }
  65. else if(x == ',' || x == ']'){
  66. //temp 에 있는 거 My_dq에 넣고 temp 클리어
  67. if(temp1 != 0){
  68. My_dq.push_back(temp1);
  69. temp1 = 0;
  70. }
  71.  
  72.  
  73. }
  74. else if(isdigit(x)){
  75. if(temp1 ==0){
  76. temp1 = x - '0';
  77. }
  78. else{
  79. temp1 *= 10;
  80. temp1 = temp1 + (x - '0');
  81. }
  82. }
  83.  
  84. }
  85.  
  86. if(My_dq.empty()){
  87. cout << "error\n";
  88. continue;
  89. }
  90.  
  91.  
  92.  
  93. //
  94. for(char c: command){
  95. if(c =='R'){
  96. reversed = !reversed;
  97. }
  98.  
  99. else if( c == 'D'){
  100. if(My_dq.empty()){
  101. cout << "error\n";
  102. break;
  103. }
  104. if(reversed) My_dq.pop_back();
  105. else My_dq.pop_front();
  106. }
  107.  
  108.  
  109.  
  110. }
  111.  
  112. if(!My_dq.empty()){
  113. cout << '[';
  114.  
  115. while (!My_dq.empty())
  116. {
  117. if(reversed){
  118. cout << My_dq.back();
  119. My_dq.pop_back();
  120.  
  121. if (My_dq.size() != 0)
  122. {
  123. cout << ',';
  124. }
  125. }
  126.  
  127. else{
  128. cout << My_dq.front();
  129. My_dq.pop_front();
  130.  
  131. if (My_dq.size() != 0)
  132. {
  133. cout << ',';
  134. }
  135. }
  136.  
  137. }
  138. cout << "]\n";
  139. }
  140.  
  141.  
  142. }
  143.  
  144. return 0;
  145. }
Success #stdin #stdout 0.01s 5328KB
stdin
2
D
1
[1]
R
0
[]
stdout
error