fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string xuly(string s){
  4. string res;
  5. stack<int> st;
  6. for(int i = 0; i < s.size();i++){
  7. if(s[i] == 'D') st.push(i + 1);
  8. else{
  9. st.push(i + 1);
  10. string tmp;
  11. while(!st.empty()){
  12. tmp += st.top();
  13. st.pop();
  14. }
  15. res += tmp;
  16.  
  17. }
  18. while(!st.empty()){
  19. res += st.top();
  20. st.pop();
  21.  
  22. }
  23. return res;
  24. }
  25. }
  26. int main(){
  27. ios_base::sync_with_stdio(false);
  28. cin.tie(0);
  29. long long t;
  30. cin >> t;
  31. while(t--){
  32. string s;
  33. cin >> s;
  34. cout << xuly(s) << "\n";
  35. }
  36. }
Success #stdin #stdout 0.01s 5320KB
stdin
4
I
D
DD
DDIDDIID
stdout