fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <stack>
  4. #include <vector>
  5. #include <string.h>
  6.  
  7. using namespace std;
  8.  
  9. vector<char> v;
  10. char arr[1000001];
  11. stack<char> st;
  12.  
  13. int main() {
  14.  
  15. int t; scanf("%d\n", &t);
  16. while(t--){
  17. memset(arr, 0, 1000001);
  18. v.clear();
  19. int cursur = 0;
  20. string s;
  21. getline(cin, s);
  22. v.resize(s.length());
  23. for(int i = 0; i < s.length(); i++){
  24. if(s[i] == '<'){
  25. if(cursur==0) continue;
  26. cursur--;
  27. }else if(s[i] == '>'){
  28. if(cursur == st.size()) continue;
  29. cursur++;
  30. }else if(s[i] == '-'){
  31. if(cursur == 0 || st.empty()) continue;
  32. int a = st.size();
  33. int cnt = 0;
  34. while(1){
  35. if(cursur == a){
  36. st.pop();
  37. for(int j = cnt-1; j >= 0; j--){
  38. st.push(v[j]);
  39. }
  40. v.clear();
  41. break;
  42. }else{
  43. v[cnt] = st.top();
  44. st.pop();
  45. cnt++;
  46. a--;
  47. }
  48. }
  49. }else{
  50. if(cursur == st.size()){
  51. st.push(s[i]);
  52. cursur += 1;
  53. }else{
  54. if(st.empty()) continue;
  55.  
  56. int a = st.size();
  57. int cnt = 0;
  58. while(1){
  59. if(cursur == a){
  60. st.push(s[i]);
  61. cursur++;
  62. for(int j = cnt-1; j >= 0; j--){
  63. st.push(v[j]);
  64. }
  65. v.clear();
  66. break;
  67. }else{
  68. v[cnt] = st.top();
  69. st.pop();
  70. cnt++;
  71. a--;
  72. }
  73. }
  74. }
  75.  
  76. }
  77. }
  78. int len = st.size();
  79.  
  80. for(int u = 0; u < len; u++){
  81. arr[u] = st.top();
  82. st.pop();
  83. }
  84. for(int u = len-1; u >= 0; u--){
  85. cout << arr[u];
  86. }
  87. cout << "\n";
  88. }
  89.  
  90. return 0;
  91. }
Success #stdin #stdout 0s 4576KB
stdin
2
<<-P<A>>Cd-
ThIsIsS3Cr3t
stdout
APC
ThIsIsS3Cr3t