fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. signed main() {
  5. ios_base::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int tt;
  9. cin >> tt;
  10. auto prec = [](char c) {
  11. return (c == '+' || c == '-' ? 1 : 2);
  12. };
  13. while (tt--) {
  14. string str;
  15. cin >> str;
  16. string ans = "";
  17. stack<char> p;
  18. for (auto c : str) {
  19. if (isalpha(c) || isdigit(c))
  20. ans += c;
  21. else if (c == '(' || c == '^')
  22. p.push(c);
  23. else if (c == ')') {
  24. while (!p.empty() && p.top() != '(') {
  25. ans += p.top();
  26. p.pop();
  27. }
  28. if (!p.empty())
  29. p.pop();
  30. } else {
  31. while (!p.empty() && p.top() != '(' && prec(c) <= prec(p.top())) {
  32. ans += p.top();
  33. p.pop();
  34. }
  35. p.push(c);
  36. }
  37. }
  38. while (!p.empty()) {
  39. ans += p.top();
  40. p.pop();
  41. }
  42. cout << ans << "\n";
  43. }
  44. }
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout