fork download
  1. using namespace std;
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <string>
  5. #include <vector>
  6. #include <stack>
  7. #include <queue>
  8. #include <iostream>
  9. #include <algorithm>
  10.  
  11. #define all(c) (c).begin(),(c).end()
  12. #define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
  13. typedef long long ll;
  14. typedef pair<int,int> pii;
  15. #define FOR(i,n) for (int i = 0; i < n; i++)
  16. #define SZ(x) ((int)x.size())
  17. #define PB push_back
  18. #define sf(x) scanf("%d",&x)
  19. #define pf(x) printf("%d\n",x)
  20. #define split(str) {vs.clear();istringstream ss(str);while(ss>>(str))vs.push_back(str);}
  21. struct node
  22. {
  23. bool type;
  24. node *left;
  25. string op;
  26. node *right;
  27. node(string c, node *pl, node *pr) : op(c), left(pl), right(pr), type(false) {}
  28. node(string c) : op(c), type(true) {}
  29. };
  30. int main()
  31. {
  32. int t;
  33. sf(t);
  34. while(t--)
  35. {
  36. string s;
  37. cin>>s;
  38. //getline(cin, s);
  39. stack<node*> mystack;
  40. FOR(i,s.size())
  41. {
  42. if(s[i]>='A'&&s[i]<='Z')
  43. {
  44. node *r=mystack.top();
  45. mystack.pop();
  46. node *l=mystack.top();
  47. mystack.pop();
  48. char buf[10];
  49. sprintf(buf,"%c",s[i]);
  50. mystack.push(new node((string)buf,l,r));
  51. continue;
  52. }
  53. else
  54. {
  55.  
  56. char buf[10];
  57. sprintf(buf,"%c",s[i]);
  58. mystack.push(new node((string)buf));
  59.  
  60. }
  61. }
  62. //cout<<"hello"<<endl;
  63. queue<node*> q;
  64. q.push(mystack.top());
  65. string ans="";
  66. while(!q.empty())
  67. {
  68. struct node *n=q.front();
  69. q.pop();
  70. if(n->type)
  71. ans=n->op+ans;
  72. else
  73. {
  74. ans=n->op+ans;
  75. q.push(n->left);
  76. q.push(n->right);
  77. }
  78. }
  79. char *fileName = (char*)ans.c_str();
  80. printf("%s\n",fileName);
  81. //cout<<ans<<endl;
  82. }
  83. }
Success #stdin #stdout 0.02s 2824KB
stdin
2
xyPzwIM
abcABdefgCDEF
stdout
wzyxIPM
gfCecbDdAaEBF