Advertisement
bibaboba12345

Untitled

Jul 21st, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <bitset>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <map>
  7. #include <set>
  8. #include <cassert>
  9.  
  10. const int N = 1e5 + 7;
  11. using namespace std;
  12. long long a[N], n, pref_left[N], pref_right[N], q;
  13.  
  14. string s, s1, s2;
  15.  
  16. bool CheckGood(string s){
  17. int bal = 0;
  18. for (int i = 0; i < s.size(); i++) {
  19. if (s[i] == '(') {
  20. bal++;
  21. }
  22. else {
  23. bal--;
  24. if (bal < 0) {
  25. return 0;
  26. }
  27. }
  28. }
  29. return bal == 0;
  30. }
  31.  
  32. void solve() {
  33. cin >> s;
  34. n = s.size();
  35. int bal = 0, x = 0, y = 0, z = 0;
  36. for (auto u : s) {
  37. if (u == '(') {
  38. bal++;
  39. }
  40. if (u == ')') {
  41. bal--;
  42. }
  43. if (u == '?') {
  44. z++;
  45. }
  46. }
  47. if (z == 1) {
  48. cout << "YES\n";
  49. return;
  50. }
  51. y = (bal + z) / 2;
  52. x = z - y;
  53. if (x == 0 || y == 0) {
  54. cout << "YES\n";
  55. return;
  56. }
  57. s1 = s;
  58. bool f = 0;
  59. int ind1 = -1, ind2 = -1;
  60. for (int i = 0; i < n; i++) {
  61. if (s1[i] == '?') {
  62. if (x) {
  63. x--;
  64. s1[i] = '(';
  65. if (x == 0) {
  66. ind1 = i;
  67. f = 1;
  68. }
  69. }
  70. else {
  71. s1[i] = ')';
  72. if (f) {
  73. ind2 = i;
  74. f = 0;
  75. }
  76. }
  77. }
  78. }
  79. swap(s1[ind1], s1[ind2]);
  80. if (CheckGood(s1)) {
  81. cout << "NO\n";
  82. return;
  83. }
  84. else {
  85. cout << "YES\n";
  86. }
  87. }
  88.  
  89. signed main()
  90. {
  91. #ifdef _DEBUG
  92. freopen("input.txt", "r", stdin);
  93. #else
  94. std::ios::sync_with_stdio(false);
  95. cin.tie(0);
  96. #endif
  97. int t;
  98. cin >> t;
  99. while (t--) {
  100. solve();
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement