fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. string s;
  5. int i,j,k,n;
  6. cin>>s;
  7. string u="";
  8. vector<int>hash(27,0);
  9. stack<char>st;
  10. i=0;
  11. for(i=0;i<s.size();i++)
  12. hash[s[i]-'a'+1]++;
  13. i=0;
  14. int check;
  15. while(i<s.size()){
  16. check=0;
  17. for(j=0;j<s[i]-'a'+1;j++)
  18. {
  19. if(hash[j]!=0) {check=1;break;}
  20. }
  21. if(check==1)
  22. { st.push(s[i]);
  23. hash[s[i]-'a'+1]--;
  24. i++;
  25. }
  26. else {u+=s[i]; hash[s[i]-'a'+1]--;i++;}
  27. }
  28. while(!st.empty())
  29. {
  30. u+=st.top();
  31. st.pop();
  32. }
  33. cout<<u;
  34. return 0;
  35. }
Success #stdin #stdout 0s 4296KB
stdin
cab
stdout
abc