Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <bitset>
- #include <vector>
- #include <algorithm>
- #include <map>
- #include <set>
- #include <cassert>
- const int N = 1e5 + 7;
- using namespace std;
- long long a[N], n, pref_left[N], pref_right[N], q;
- string s, s1, s2;
- bool CheckGood(string s){
- int bal = 0;
- for (int i = 0; i < s.size(); i++) {
- if (s[i] == '(') {
- bal++;
- }
- else {
- bal--;
- if (bal < 0) {
- return 0;
- }
- }
- }
- return bal == 0;
- }
- void solve() {
- cin >> s;
- n = s.size();
- int bal = 0, x = 0, y = 0, z = 0;
- for (auto u : s) {
- if (u == '(') {
- bal++;
- }
- if (u == ')') {
- bal--;
- }
- if (u == '?') {
- z++;
- }
- }
- if (z == 1) {
- cout << "YES\n";
- return;
- }
- y = (bal + z) / 2;
- x = z - y;
- if (x == 0 || y == 0) {
- cout << "YES\n";
- return;
- }
- s1 = s;
- bool f = 0;
- int ind1 = -1, ind2 = -1;
- for (int i = 0; i < n; i++) {
- if (s1[i] == '?') {
- if (x) {
- x--;
- s1[i] = '(';
- if (x == 0) {
- ind1 = i;
- f = 1;
- }
- }
- else {
- s1[i] = ')';
- if (f) {
- ind2 = i;
- f = 0;
- }
- }
- }
- }
- swap(s1[ind1], s1[ind2]);
- if (CheckGood(s1)) {
- cout << "NO\n";
- return;
- }
- else {
- cout << "YES\n";
- }
- }
- signed main()
- {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- #else
- std::ios::sync_with_stdio(false);
- cin.tie(0);
- #endif
- int t;
- cin >> t;
- while (t--) {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement