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.size(), 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. for (int i = 0; i < m; i++) {
  35. if (s[i] == '(') {
  36. pa.push_back({cnt, i});
  37. cnt++;
  38. }
  39. else if (s[i] == ')') {
  40. cnt--;
  41. pa.push_back({cnt, i});
  42. }
  43. }
  44.  
  45. queue<string> q;
  46. queue<string> q1;
  47. for (int i = 0; i < n; i++) {
  48. q.push(to_string(i));
  49. q1.push(to_string(i));
  50. }
  51.  
  52. while (q.front().size() < n) {
  53. string s1 = q.front();
  54. q.pop();
  55. int chusocuoicungs1 = (s1.back() - '0');
  56. for (int i = chusocuoicungs1 + 1; i < n; i++) {
  57. q.push(s1 + to_string(i));
  58. q1.push(s1 + to_string(i));
  59. }
  60. }
  61.  
  62. set<string> ans;
  63. while (!q1.empty()) {
  64. string s1 = q1.front();
  65. q1.pop();
  66. ans.insert(solve(s, s1));
  67. }
  68. for (auto x : ans) {
  69. cout << x << endl;
  70. }
  71. }
  72.  
  73. int main () {
  74. ios_base::sync_with_stdio(false);
  75. cin.tie(NULL); cout.tie(NULL);
  76.  
  77. int T = 1;
  78. //cin >> T;
  79. while (T--) {
  80. TC();
  81. }
  82. return 0;
  83. }
Success #stdin #stdout 0.01s 5316KB
stdin
(1+(2)*3)+(4)
stdout
(1+(2)*3)+(4)
(1+2*3)+(4)
1+(2)*3+4
1+2*3+4