fork download
  1. #include <iostream>
  2. #include <deque>
  3. using namespace std;
  4.  
  5. int main() {
  6. ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  7. int T, n, Cnt{ 0 };
  8. bool check{ false }, reversed{ false };
  9. string p, s, tempS;
  10. cin >> T;
  11.  
  12. for (int i{ 0 }; i < T; i++) {
  13. deque<string> DQ;
  14. cin >> p >> n >> s;
  15.  
  16. for (int j{ 0 }, i{ 0 }; i < n; i++) {
  17. while (s[j] != ',' && s[j] != ']') {
  18. if (s[j] != '[') tempS += s[j];
  19. j++;
  20. }
  21. if (tempS != "") {
  22. DQ.push_back(tempS);
  23. tempS = "";
  24. }
  25. j++;
  26. }//덱에 숫자 push
  27.  
  28. for (int j{ 0 }; j < p.size(); j++) {
  29. while (j < p.size() && p[j] == 'R') {
  30. Cnt++;
  31. j++;
  32. }
  33. if (Cnt % 2) {
  34. if (reversed) reversed = false;
  35. else reversed = true;
  36. }
  37. if (p[j] == 'D') {
  38. if (!DQ.empty()) {
  39. if (reversed) DQ.pop_back();
  40. else DQ.pop_front();
  41. }
  42. else {
  43. check = true;
  44. break;
  45. }
  46. }
  47. Cnt = 0;
  48. }//R,D 처리
  49.  
  50. if (check) {
  51. cout << "error\n";
  52. check = false;
  53. continue;
  54. }//비었는데 삭제 시도할 시 error 출력
  55.  
  56. if (DQ.empty()) {
  57. cout << "[]\n";
  58. }//비었으면 [] 출력
  59.  
  60. else if (!reversed) {
  61. cout << "[";
  62. for (int i{ 0 }; i < DQ.size() - 1; i++) {
  63. cout << DQ[i] << ",";
  64. }
  65. cout << DQ[DQ.size() - 1] << "]\n";
  66. }
  67. else {
  68. cout << "[";
  69. for (size_t i{ DQ.size() - 1 }; i > 0; i--) {
  70. cout << DQ[i] << ",";
  71. }
  72. cout << DQ[0] << "]\n";
  73. }
  74. reversed = false;
  75. }
  76. }
Success #stdin #stdout 0.01s 5432KB
stdin
2
RDD
1
[1]
R
2
[1,2]
stdout
error
[1,2]