fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. vector<pair<int, int> > pa;
  5.  
  6. string solve(string s, string s1) { // xoa cac dau ngoac co chi so s1 trong xau s;
  7. vector<int> dd(s.length(), 1);
  8. for (int i = 0; i < s1.size(); i++) {
  9. for (auto x : pa) {
  10. if (x.first == s1[i] - '0') {
  11. dd[x.second] = 0;
  12. }
  13. }
  14. }
  15. string res = "";
  16. for (int i = 0; i < s.size(); ++i) {
  17. if (dd[i]) res += s[i];
  18. }
  19. return res;
  20. }
  21.  
  22. void TC () {
  23. string s;
  24. cin >> s;
  25. int m = s.size();
  26. int n = 0;
  27. for (int i = 0; i < m; i++) {
  28. if (s[i] == '(') {
  29. n++;
  30. }
  31. }
  32.  
  33. int cnt = 0;
  34. stack<pair<int, int> > st;
  35. for (int i = 0; i < m; i++) { // ()()(()())
  36. if (s[i] == '(') {
  37. st.push({cnt, i}); // (1+(2*(3+4)))
  38. pa.push_back({cnt, i});
  39. cnt++;
  40. }
  41. else if (s[i] == ')' && !st.empty()) {
  42. int x = st.top().first;
  43. st.pop();
  44. pa.push_back({x, i});
  45. }
  46. }
  47. // for (auto x : pa) {
  48. // cout << x.first << " " << x.second << endl;
  49. // }
  50. // tao queue luu nhung vi tri can xoa;
  51.  
  52. queue<string> q;
  53. queue<string> q1; // q1 giong q nhung khong xoa di phan tu dau tien, chi push them vao thoi;
  54. for (int i = 0; i < n; i++) {
  55. q.push(to_string(i));
  56. q1.push(to_string(i));
  57. }
  58.  
  59. while (q.front().size() < n) { // 123 23 13 12 3 2 1;
  60. string s1 = q.front();
  61. q.pop();
  62. int chusocuoicungs1 = (s1.back() - '0');
  63. for (int i = chusocuoicungs1 + 1; i < n; i++) {
  64. q.push(s1 + to_string(i));
  65. q1.push(s1 + to_string(i));
  66. }
  67. }
  68.  
  69. map<string, int> ans;
  70. while (!q1.empty()) {
  71. string s1 = q1.front();
  72. q1.pop();
  73. // ans.push_back(solve(s, s1));
  74. ans[solve(s, s1)] = 1;
  75. }
  76. // sort(ans.begin(), ans.end());
  77. // for (auto x : ans) {
  78. // cout << x << endl;
  79. // }
  80. for (auto i : ans) cout << i.first << endl;
  81. }
  82.  
  83. int main () {
  84. ios_base::sync_with_stdio(false);
  85. cin.tie(NULL); cout.tie(NULL);
  86.  
  87. int T = 1;
  88. //cin >> T;
  89. while (T--) {
  90. TC();
  91. }
  92. return 0;
  93. }
Success #stdin #stdout 0.01s 5424KB
stdin
(1+(2)*3)+(4)
stdout
(1+(2)*3)+4
(1+2*3)+(4)
(1+2*3)+4
1+(2)*3+(4)
1+(2)*3+4
1+2*3+(4)
1+2*3+4