fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define fi first
  5. #define se second
  6. #define siz(x) (int)(x.size())
  7. #define all(x) x.begin(), x.end()
  8. #define debug_arr(x,len) for(int _=1; _<=len; _++) cout<<x[_]<<" "; cout<<'\n';
  9. #define debug(x) cout<<'\n'<<#x<<": "<<x<<'\n';
  10. const int maxN = 2e5+5;
  11.  
  12. int n;
  13. bool good[maxN], tru[maxN], cong[maxN];
  14. string s;
  15. vector<pair<int,int>>v;
  16. void solve()
  17. {
  18.  
  19. }
  20.  
  21. int32_t main()
  22. {
  23. ios_base::sync_with_stdio(0); cin.tie(0);
  24. cin>>s;
  25. n = siz(s);
  26. stack<int>st;
  27. map<int, bool>minus, plus;
  28. for(int i=0; i<n; i+=1)
  29. {
  30. if(s[i] == '(') st.push(i);
  31. if(s[i] == ')')
  32. {
  33. // cout<<st.top()<<" "<<i<<" "<<'\n';
  34. v.push_back({st.top(), i});
  35. st.pop();
  36. }
  37. }
  38. for(int i=0; i<n; i+=1)
  39. {
  40. if(s[i] == '-') minus[i] = 1;
  41. if(s[i] == '+') plus[i] = 1;
  42. }
  43. for(int i=0; i<siz(v); i+=1)
  44. {
  45. vector<int>xoa_cong, xoa_tru;
  46. auto it = minus.lower_bound(v[i].fi);
  47. while(it != minus.end() && it->fi <= v[i].se)
  48. {
  49. tru[v[i].fi] = tru[v[i].se] = 1;
  50. xoa_tru.push_back(it->fi);
  51. it++;
  52. }
  53. it = plus.lower_bound(v[i].fi);
  54. while(it != plus.end() && it->fi <= v[i].se)
  55. {
  56. cong[v[i].fi] = cong[v[i].se] = 1;
  57. xoa_cong.push_back(it->fi);
  58. it++;
  59. }
  60. for(auto j: xoa_tru) minus.erase(j);
  61. for(auto j: xoa_cong) plus.erase(j);
  62. }
  63. for(int i=0; i<siz(v); i+=1)
  64. {
  65. if(!tru[v[i].fi]) continue;
  66. bool checkl = 0, checkr = 0;
  67. if(v[i].fi == 0) checkl = 1;
  68. if(v[i].fi != 0 && s[v[i].fi-1] == '+') checkl = 1;
  69. if(v[i].fi != 0 && s[v[i].fi-1] == '(') checkl = 1;
  70. if(v[i].se == n-1) checkr = 1;
  71. if(v[i].se != n-1 && s[v[i].se+1] == '+') checkr = 1;
  72. if(v[i].se != n-1 && s[v[i].se+1] == '-') checkr = 1;
  73. if(v[i].se != n-1 && s[v[i].se+1] == ')') checkr = 1;
  74. if(checkl && checkr) good[v[i].fi] = good[v[i].se] = 1;
  75. }
  76. for(int i=0; i<siz(v); i+=1)
  77. {
  78. if(!cong[v[i].fi]) continue;
  79. bool checkl = 0, checkr = 0;
  80. if(v[i].fi == 0) checkl = 1;
  81. if(v[i].fi != 0 && s[v[i].fi-1] == '+') checkl = 1;
  82. if(v[i].fi != 0 && s[v[i].fi-1] == '(') checkl = 1;
  83. if(v[i].se == n-1) checkr = 1;
  84. if(v[i].se != n-1 && s[v[i].se+1] == '+') checkr = 1;
  85. if(v[i].se != n-1 && s[v[i].se+1] == '-') checkr = 1;
  86. if(v[i].se != n-1 && s[v[i].se+1] == ')') checkr = 1;
  87. if(checkl && checkr) good[v[i].fi] = good[v[i].se] = 1;
  88. }
  89. for(int i=0; i<siz(v); i+=1)
  90. {
  91. if(cong[v[i].fi]) continue;
  92. if(tru[v[i].fi]) continue;
  93. good[v[i].fi] = good[v[i].se] = 1;
  94. }
  95. for(int i=0; i<n; i+=1)
  96. {
  97. if(good[i]) continue;
  98. cout<<s[i];
  99. }
  100. }
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
Standard output is empty