Advertisement
a53

StringPushPop

a53
May 24th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <deque>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. ios_base::sync_with_stdio(false);
  8. cin.tie(0), cout.tie(0);
  9. string s;
  10. cin>>s;
  11. deque <char> dq;
  12. int i=0;
  13. while(s[i]!='\0')
  14. dq.push_back(s[i++]);
  15. int q;
  16. cin>>q;
  17. int tip_op;
  18. char c;
  19. bool rev=true;
  20. while(q--)
  21. {
  22. cin>>tip_op;
  23. if(tip_op==5)
  24. rev=!rev;
  25. else
  26. if(tip_op>2)
  27. {
  28. if((tip_op==3&&!rev)||(tip_op==4&&rev))
  29. dq.pop_back();
  30. else
  31. dq.pop_front();
  32. }
  33. else
  34. {
  35. cin>>c;
  36. if((tip_op==1&&!rev)||(tip_op==2&&rev))
  37. dq.push_back(c);
  38. else
  39. dq.push_front(c);
  40. }
  41. }
  42. s.clear();
  43. while(!dq.empty())
  44. if (rev)
  45. s+=dq.front(),dq.pop_front();
  46. else
  47. s+=dq.back(),dq.pop_back();
  48. cout<<s;
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement