output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; bool sBs(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } long long SUMD(long long n); long long BS(vector<long long> &PS, long long s, long long e, long long ser); long long MI(long long a, long long m); bool P[100000LL + 1]; void Sieve(int n = 100000LL); long long Powb(long long b, long long n); int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long q = 1; while (q--) { long long b, w, n, m, k, i, j; cin >> n; string s, s1; cin >> s; if (n % 2) { cout << ":("; return 0; } if (s[0] == ')' or s[n - 1] == '(') { cout << ":("; return 0; } s[0] = '('; s[n - 1] = ')'; s1 = s; i = 1, j = n - 2; long long cO = 0, cC = 0; while (i < j) { if (s[i] == '(' or s[i] == '?') { cO++; s[i] = '('; } else { cO--; if (cO < 0) { cout << ":("; return 0; } } if (s[j] == ')' or s[j] == '?') { cC++; s[j] = ')'; } else { cC--; if (cC < 0) { cout << ":("; return 0; } } i++; j--; } i = n / 2 - 1, j = n / 2; if (cO < cC) { while (cO != cC and j < n - 1) { while (s1[j] != '?' and j < n - 1) j++; s[j] = '('; cO++; cC--; j++; } } else if (cO > cC) { while (cO != cC and i > 0) { while (s1[i] != '?' and i > 0) i--; s[i] = ')'; cO--; cC++; i--; } } long long c = 1; for (i = 1; i < n - 1; i++) { if (s[i] == '(') c++; else c--; if (c <= 0) { cout << ":("; return 0; } } if (c == 1) cout << s << "\n"; else cout << ":("; } return 0; } long long Powb(long long b, long long n) { if (n == 0) return 1LL; if (n == 1) return b; long long temp = Powb(b, n / 2); if (n % 2 == 0) { return (temp * temp) % 1000000007; } else { return (b * ((temp * temp) % 1000000007)) % 1000000007; } } long long SUMD(long long n) { long long sum = 0; while (n > 0) { sum += n % 10; n = n / 10; } return sum; } long long BS(vector<long long> &PS, long long s, long long e, long long ser) { if (s > e) return s; long long mid = (s + e) / 2; if (PS[mid] == ser) { return mid; } else if (PS[mid] > ser) { return BS(PS, s, mid - 1, ser); } else return BS(PS, mid + 1, e, ser); } long long MI(long long a, long long m) { long long m0 = m; long long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } void Sieve(int n) { memset(P, true, sizeof(P)); P[0] = false; P[1] = false; for (long long i = 2; i * i <= n; i++) { if (P[i]) { for (long long j = i * i; j <= n; j += i) { P[j] = false; } } } }
### Prompt Please create a solution in CPP to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool sBs(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } long long SUMD(long long n); long long BS(vector<long long> &PS, long long s, long long e, long long ser); long long MI(long long a, long long m); bool P[100000LL + 1]; void Sieve(int n = 100000LL); long long Powb(long long b, long long n); int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long q = 1; while (q--) { long long b, w, n, m, k, i, j; cin >> n; string s, s1; cin >> s; if (n % 2) { cout << ":("; return 0; } if (s[0] == ')' or s[n - 1] == '(') { cout << ":("; return 0; } s[0] = '('; s[n - 1] = ')'; s1 = s; i = 1, j = n - 2; long long cO = 0, cC = 0; while (i < j) { if (s[i] == '(' or s[i] == '?') { cO++; s[i] = '('; } else { cO--; if (cO < 0) { cout << ":("; return 0; } } if (s[j] == ')' or s[j] == '?') { cC++; s[j] = ')'; } else { cC--; if (cC < 0) { cout << ":("; return 0; } } i++; j--; } i = n / 2 - 1, j = n / 2; if (cO < cC) { while (cO != cC and j < n - 1) { while (s1[j] != '?' and j < n - 1) j++; s[j] = '('; cO++; cC--; j++; } } else if (cO > cC) { while (cO != cC and i > 0) { while (s1[i] != '?' and i > 0) i--; s[i] = ')'; cO--; cC++; i--; } } long long c = 1; for (i = 1; i < n - 1; i++) { if (s[i] == '(') c++; else c--; if (c <= 0) { cout << ":("; return 0; } } if (c == 1) cout << s << "\n"; else cout << ":("; } return 0; } long long Powb(long long b, long long n) { if (n == 0) return 1LL; if (n == 1) return b; long long temp = Powb(b, n / 2); if (n % 2 == 0) { return (temp * temp) % 1000000007; } else { return (b * ((temp * temp) % 1000000007)) % 1000000007; } } long long SUMD(long long n) { long long sum = 0; while (n > 0) { sum += n % 10; n = n / 10; } return sum; } long long BS(vector<long long> &PS, long long s, long long e, long long ser) { if (s > e) return s; long long mid = (s + e) / 2; if (PS[mid] == ser) { return mid; } else if (PS[mid] > ser) { return BS(PS, s, mid - 1, ser); } else return BS(PS, mid + 1, e, ser); } long long MI(long long a, long long m) { long long m0 = m; long long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } void Sieve(int n) { memset(P, true, sizeof(P)); P[0] = false; P[1] = false; for (long long i = 2; i * i <= n; i++) { if (P[i]) { for (long long j = i * i; j <= n; j += i) { P[j] = false; } } } } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n % 2 == 1) { cout << ":("; } else { int i = 0, j = n - 1, cnt = 0; int a = n / 2, b = n / 2; for (int i = 0; i < n; i++) { if (s[i] == '(') { a--; } else if (s[i] == ')') { b--; } } for (int i = 0; i < n; i++) { if (s[i] == '?' && a > 0) { s[i] = '('; a--; } else if (s[i] == '?' && a == 0) { s[i] = ')'; a--; } } stack<char> st, et; int i1 = 0; while (i1 < s.size()) { if (s[i1] == '(') { st.push(s[i1]); } else if ((!st.empty() && st.top() == '(') && (s[i1] = ')')) { st.pop(); } else { et.push(s[i1]); } if ((st.empty()) && (i1 != n - 1) && (et.empty())) { cout << ":("; return 0; } i1++; } if (st.empty() && et.empty()) { cout << s; } else { cout << ":("; } } return 0; }
### Prompt Please formulate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n % 2 == 1) { cout << ":("; } else { int i = 0, j = n - 1, cnt = 0; int a = n / 2, b = n / 2; for (int i = 0; i < n; i++) { if (s[i] == '(') { a--; } else if (s[i] == ')') { b--; } } for (int i = 0; i < n; i++) { if (s[i] == '?' && a > 0) { s[i] = '('; a--; } else if (s[i] == '?' && a == 0) { s[i] = ')'; a--; } } stack<char> st, et; int i1 = 0; while (i1 < s.size()) { if (s[i1] == '(') { st.push(s[i1]); } else if ((!st.empty() && st.top() == '(') && (s[i1] = ')')) { st.pop(); } else { et.push(s[i1]); } if ((st.empty()) && (i1 != n - 1) && (et.empty())) { cout << ":("; return 0; } i1++; } if (st.empty() && et.empty()) { cout << s; } else { cout << ":("; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 100000007; const int MAXN = 1000000; const int inf = 0x3f3f3f3f; char str[300005]; int main() { int n; scanf("%d", &n); scanf("%s", str); int len = strlen(str), fo = 1, x = 0, sum = 0; if (len & 1) fo = 0; for (int i = 0; i < len; i++) { if (str[i] == '(') x++; else if (str[i] == ')') x--; else if (str[i] == '?') sum++; } int a = (sum - x) / 2, b = sum - a; for (int i = 0; i < len; i++) { if (str[i] == '?') { if (a > 0) { str[i] = '('; a--; } else if (b > 0) { str[i] = ')'; b--; } } } int now = 0; for (int i = 0; i < len - 1; i++) { if (str[i] == '(') now++; else if (str[i] == ')') now--; if (now == 0 || now < 0) { fo = 0; break; } } if (str[len - 1] == '(') now++; else if (str[len - 1] == ')') now--; if (now != 0) fo = 0; if (fo == 0) printf(":(\n"); else printf("%s\n", str); return 0; }
### Prompt In CPP, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 100000007; const int MAXN = 1000000; const int inf = 0x3f3f3f3f; char str[300005]; int main() { int n; scanf("%d", &n); scanf("%s", str); int len = strlen(str), fo = 1, x = 0, sum = 0; if (len & 1) fo = 0; for (int i = 0; i < len; i++) { if (str[i] == '(') x++; else if (str[i] == ')') x--; else if (str[i] == '?') sum++; } int a = (sum - x) / 2, b = sum - a; for (int i = 0; i < len; i++) { if (str[i] == '?') { if (a > 0) { str[i] = '('; a--; } else if (b > 0) { str[i] = ')'; b--; } } } int now = 0; for (int i = 0; i < len - 1; i++) { if (str[i] == '(') now++; else if (str[i] == ')') now--; if (now == 0 || now < 0) { fo = 0; break; } } if (str[len - 1] == '(') now++; else if (str[len - 1] == ')') now--; if (now != 0) fo = 0; if (fo == 0) printf(":(\n"); else printf("%s\n", str); return 0; } ```
#include <bits/stdc++.h> using namespace std; bool check(string s) { int count = 0; for (char el : s) { if (el == '(') { count++; } else { count--; } if (count < 0) return false; } return true; } void solve() { int n; cin >> n; string s; cin >> s; if (s[0] == ')' || s[s.size() - 1] == '(' || s.size() % 2) { cout << ":(" << endl; return; } int in = 0, out = 0; for (char el : s) { in += (el == '('); out += (el == ')'); } if (in > n / 2 || out > n / 2) { cout << ":(" << endl; return; } int first = (n / 2) - in; int second = (n / 2) - out; int count = 0; for (int i = 0; i < n; i++) { if (s[i] == '?') { if (first > 0) { s[i] = '('; first--; count++; } else { s[i] = ')'; second--; count--; } } else if (s[i] == '(') { count++; } else { count--; } if (count == 0 && i != n - 1) { cout << ":(" << endl; return; } else if (count < 0) { cout << ":(" << endl; return; } } cout << (check(s) ? s : ":(") << endl; } int main() { solve(); }
### Prompt Create a solution in CPP for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool check(string s) { int count = 0; for (char el : s) { if (el == '(') { count++; } else { count--; } if (count < 0) return false; } return true; } void solve() { int n; cin >> n; string s; cin >> s; if (s[0] == ')' || s[s.size() - 1] == '(' || s.size() % 2) { cout << ":(" << endl; return; } int in = 0, out = 0; for (char el : s) { in += (el == '('); out += (el == ')'); } if (in > n / 2 || out > n / 2) { cout << ":(" << endl; return; } int first = (n / 2) - in; int second = (n / 2) - out; int count = 0; for (int i = 0; i < n; i++) { if (s[i] == '?') { if (first > 0) { s[i] = '('; first--; count++; } else { s[i] = ')'; second--; count--; } } else if (s[i] == '(') { count++; } else { count--; } if (count == 0 && i != n - 1) { cout << ":(" << endl; return; } else if (count < 0) { cout << ":(" << endl; return; } } cout << (check(s) ? s : ":(") << endl; } int main() { solve(); } ```
#include <bits/stdc++.h> using namespace std; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; void shoot() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { shoot(); long long i, n, x, a = 0, b = 0; cin >> n; char s[n], t[n]; for (i = 0; i < n; i++) { cin >> s[i]; if (s[i] == '(') a++; else if (s[i] == ')') b++; } if (n % 2 || (n / 2 - a) < 0 || (n / 2 - b) < 0) { cout << ":("; return 0; } x = n / 2 - a; for (i = 0; i < n; i++) { if (s[i] == '?') { if (x) t[i] = '(', x--; else t[i] = ')'; } else t[i] = s[i]; } a = b = 0; for (i = 0; i < n; i++) { if (t[i] == '(') a++; else b++; if (b >= a && i != n - 1) { cout << ":("; return 0; } } for (i = 0; i < n; i++) cout << t[i]; return 0; }
### Prompt Generate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; void shoot() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { shoot(); long long i, n, x, a = 0, b = 0; cin >> n; char s[n], t[n]; for (i = 0; i < n; i++) { cin >> s[i]; if (s[i] == '(') a++; else if (s[i] == ')') b++; } if (n % 2 || (n / 2 - a) < 0 || (n / 2 - b) < 0) { cout << ":("; return 0; } x = n / 2 - a; for (i = 0; i < n; i++) { if (s[i] == '?') { if (x) t[i] = '(', x--; else t[i] = ')'; } else t[i] = s[i]; } a = b = 0; for (i = 0; i < n; i++) { if (t[i] == '(') a++; else b++; if (b >= a && i != n - 1) { cout << ":("; return 0; } } for (i = 0; i < n; i++) cout << t[i]; return 0; } ```
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; using ll = long long; using ull = unsigned long long; bool check(char a, char b) { return (a == '(' && b == ')') || (a == '(' && b == '?') || (a == '?' && b == ')') || (a == '?' && b == '?'); } bool solve(string& s) { const int n = (int)s.length(); if (n % 2 == 1) return false; if (!check(s[0], s[n - 1])) return false; s[0] = '('; s[n - 1] = ')'; int opened = (int)count(s.begin(), s.end(), '('); for (int i = 1; i < n - 1; ++i) { if (s[i] == '?') { if (opened < n / 2) { ++opened; s[i] = '('; } else { s[i] = ')'; } } } int stack = 0; for (int i = 1; i < n - 1; ++i) { if (s[i] == '(') ++stack; else if (stack > 0) --stack; else return false; } return stack == 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; if (solve(s)) cout << s << endl; else cout << ":(" << endl; return 0; }
### Prompt In cpp, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; using ll = long long; using ull = unsigned long long; bool check(char a, char b) { return (a == '(' && b == ')') || (a == '(' && b == '?') || (a == '?' && b == ')') || (a == '?' && b == '?'); } bool solve(string& s) { const int n = (int)s.length(); if (n % 2 == 1) return false; if (!check(s[0], s[n - 1])) return false; s[0] = '('; s[n - 1] = ')'; int opened = (int)count(s.begin(), s.end(), '('); for (int i = 1; i < n - 1; ++i) { if (s[i] == '?') { if (opened < n / 2) { ++opened; s[i] = '('; } else { s[i] = ')'; } } } int stack = 0; for (int i = 1; i < n - 1; ++i) { if (s[i] == '(') ++stack; else if (stack > 0) --stack; else return false; } return stack == 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; if (solve(s)) cout << s << endl; else cout << ":(" << endl; return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC tsmthget("avx2") using namespace std; const long long one = 1; const long double INF = 2e18; const long long MOD = 1e9 + 7; const long long N = 2010; const long long nul = 0; signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); long long n; cin >> n; string s; cin >> s; long long ball = 0, bal = 0; for (auto i : s) { if (i == '(') ball++; else if (i == ')') ball--; } vector<long long> pref(n + 1, 0); for (long long i = 1; i <= n; i++) { if (s[i - 1] == ')') pref[i] = pref[i - 1] - 1; else pref[i] = pref[i - 1] + 1; } bool flag = 0; if (pref[n] < 0) flag = 1; for (long long i = 1; i < n; i++) { if (pref[i] <= 0) flag = 1; } if (flag) { cout << ":(" << endl; return 0; } if (pref[n] != 0) { long long x = pref[n]; if (pref[n] % 2 == 1) flag = 1; for (long long i = n; i > 0; i--) { if (s[i - 1] == '?') { x -= 2; s[i - 1] = ')'; } if (x == 0) break; } for (char &i : s) { if (i == '?') i = '('; } if (x != 0) flag = 1; bal = 0; for (long long i = 0; i < n - 1; i++) { if (s[i] == '(') bal++; else bal--; if (bal <= 0) flag = 1; } if (flag) cout << ":(" << endl; else cout << s << endl; } else { for (char &i : s) { if (i == '?') i = '('; } cout << s << endl; } }
### Prompt Your task is to create a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC tsmthget("avx2") using namespace std; const long long one = 1; const long double INF = 2e18; const long long MOD = 1e9 + 7; const long long N = 2010; const long long nul = 0; signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); long long n; cin >> n; string s; cin >> s; long long ball = 0, bal = 0; for (auto i : s) { if (i == '(') ball++; else if (i == ')') ball--; } vector<long long> pref(n + 1, 0); for (long long i = 1; i <= n; i++) { if (s[i - 1] == ')') pref[i] = pref[i - 1] - 1; else pref[i] = pref[i - 1] + 1; } bool flag = 0; if (pref[n] < 0) flag = 1; for (long long i = 1; i < n; i++) { if (pref[i] <= 0) flag = 1; } if (flag) { cout << ":(" << endl; return 0; } if (pref[n] != 0) { long long x = pref[n]; if (pref[n] % 2 == 1) flag = 1; for (long long i = n; i > 0; i--) { if (s[i - 1] == '?') { x -= 2; s[i - 1] = ')'; } if (x == 0) break; } for (char &i : s) { if (i == '?') i = '('; } if (x != 0) flag = 1; bal = 0; for (long long i = 0; i < n - 1; i++) { if (s[i] == '(') bal++; else bal--; if (bal <= 0) flag = 1; } if (flag) cout << ":(" << endl; else cout << s << endl; } else { for (char &i : s) { if (i == '?') i = '('; } cout << s << endl; } } ```
#include <bits/stdc++.h> using namespace std; const long long MAXN = 300100; const long long INF = 2000000100; const long long MOD = 998244353; int N; string S, ans; int main() { ios_base::sync_with_stdio(0); cin >> N >> S; if (N % 2 == 1) { cout << ":(\n"; return 0; } int l = 0, c = 0; bool m = 0; for (int i = 0; i < N; i++) { if (S[i] == '(') l++; } for (int i = 0; i < N; i++) { if (l >= N / 2) m = 1; if (c <= 0 && i != 0) { cout << ":(\n"; return 0; } if (S[i] == '(') { c++; ans += '('; } else if (S[i] == ')') { c--; ans += ')'; } else { if (m) { c--; ans += ')'; } else { c++; l++; ans += '('; } } } if (c > 0) { cout << ":(\n"; return 0; } cout << ans << "\n"; }
### Prompt In CPP, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MAXN = 300100; const long long INF = 2000000100; const long long MOD = 998244353; int N; string S, ans; int main() { ios_base::sync_with_stdio(0); cin >> N >> S; if (N % 2 == 1) { cout << ":(\n"; return 0; } int l = 0, c = 0; bool m = 0; for (int i = 0; i < N; i++) { if (S[i] == '(') l++; } for (int i = 0; i < N; i++) { if (l >= N / 2) m = 1; if (c <= 0 && i != 0) { cout << ":(\n"; return 0; } if (S[i] == '(') { c++; ans += '('; } else if (S[i] == ')') { c--; ans += ')'; } else { if (m) { c--; ans += ')'; } else { c++; l++; ans += '('; } } } if (c > 0) { cout << ":(\n"; return 0; } cout << ans << "\n"; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, sum, st, ed; string s; cin >> n >> s; sum = 0; st = ed = n / 2; for (int i = 0; i < n; i++) { if (s[i] == '(') st--; else if (s[i] == ')') ed--; } if (n % 2 || st < 0 || ed < 0 || s[0] == ')' || s[n - 1] == '(') { cout << ":(" << endl; exit(0); } if (s[0] == '?') s[0] = '(', st--; if (s[n - 1] == '?') s[n - 1] = ')', ed--; for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { if (st > 0) s[i] = '(', st--; else s[i] = ')', ed--; } if (s[i] == '(') sum++; else sum--; if (sum < 0) { cout << ":(" << endl; exit(0); } } if (sum == 0) cout << s << endl; else cout << ":(" << endl; }
### Prompt Create a solution in CPP for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, sum, st, ed; string s; cin >> n >> s; sum = 0; st = ed = n / 2; for (int i = 0; i < n; i++) { if (s[i] == '(') st--; else if (s[i] == ')') ed--; } if (n % 2 || st < 0 || ed < 0 || s[0] == ')' || s[n - 1] == '(') { cout << ":(" << endl; exit(0); } if (s[0] == '?') s[0] = '(', st--; if (s[n - 1] == '?') s[n - 1] = ')', ed--; for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { if (st > 0) s[i] = '(', st--; else s[i] = ')', ed--; } if (s[i] == '(') sum++; else sum--; if (sum < 0) { cout << ":(" << endl; exit(0); } } if (sum == 0) cout << s << endl; else cout << ":(" << endl; } ```
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 100; const long long inf = 0x3f3f3f3f; int32_t main() { long long n; scanf("%I64d", &(n)); string s; cin >> s; long long l = n / 2, r = n / 2; long long a = 0, b = 0; if (s[0] == ')' || s[n - 1] == '(') { printf(":("); return 0; } for (long long i = (0); i <= (n - 1); i++) if (s[i] == '(') a++; for (long long i = (0); i <= (n - 1); i++) if (s[i] == '?') { if (a < l) { s[i] = '('; a++; } else s[i] = ')'; } a = 0; for (long long i = (0); i <= (n - 1); i++) { if (s[i] == '(') a++; else b++; if (a == b && i != n - 1) { printf(":("); return 0; } } if (a != b) printf(":("); else cout << s; return 0; }
### Prompt Please create a solution in cpp to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 100; const long long inf = 0x3f3f3f3f; int32_t main() { long long n; scanf("%I64d", &(n)); string s; cin >> s; long long l = n / 2, r = n / 2; long long a = 0, b = 0; if (s[0] == ')' || s[n - 1] == '(') { printf(":("); return 0; } for (long long i = (0); i <= (n - 1); i++) if (s[i] == '(') a++; for (long long i = (0); i <= (n - 1); i++) if (s[i] == '?') { if (a < l) { s[i] = '('; a++; } else s[i] = ')'; } a = 0; for (long long i = (0); i <= (n - 1); i++) { if (s[i] == '(') a++; else b++; if (a == b && i != n - 1) { printf(":("); return 0; } } if (a != b) printf(":("); else cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; int cnt = 0; for (int i = 0; i < n; ++i) { if (s[i] == '(') cnt++; } if (n % 2 || cnt > n / 2) { cout << ":(" << endl; return 0; } int req = n / 2 - cnt; for (int i = 0; i < n; ++i) { if (req <= 0) break; if (s[i] == '?') { s[i] = '('; req--; } } for (int i = 0; i < n; ++i) { if (s[i] == '?') { s[i] = ')'; } } cnt = 0; for (int i = 0; i < n; ++i) { if (s[i] == '(') cnt++; else cnt--; if (cnt < 0) { cout << ":(" << endl; return 0; } if (i != n - 1 && cnt == 0) { cout << ":(" << endl; return 0; } } if (cnt != 0) { cout << ":(" << endl; return 0; } cout << s << endl; return 0; }
### Prompt Please create a solution in CPP to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; int cnt = 0; for (int i = 0; i < n; ++i) { if (s[i] == '(') cnt++; } if (n % 2 || cnt > n / 2) { cout << ":(" << endl; return 0; } int req = n / 2 - cnt; for (int i = 0; i < n; ++i) { if (req <= 0) break; if (s[i] == '?') { s[i] = '('; req--; } } for (int i = 0; i < n; ++i) { if (s[i] == '?') { s[i] = ')'; } } cnt = 0; for (int i = 0; i < n; ++i) { if (s[i] == '(') cnt++; else cnt--; if (cnt < 0) { cout << ":(" << endl; return 0; } if (i != n - 1 && cnt == 0) { cout << ":(" << endl; return 0; } } if (cnt != 0) { cout << ":(" << endl; return 0; } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main(void) { { ios_base::sync_with_stdio(false); cin.tie(NULL); }; int n, i; cin >> n; string s; cin >> s; if (n & 1) cout << ":("; else { int a = n / 2, b = n / 2, c = 0, bad = 0; for (i = 0; i < n; i++) { if (s[i] == '(') a--; if (s[i] == ')') b--; } for (i = 0; i < n; i++) { if (a <= 0) break; if (s[i] == '?') { s[i] = '('; a--; } } for (i = 0; i < n; i++) { if (b <= 0) break; if (s[i] == '?') { s[i] = ')'; b--; } } for (i = 0; i < n; i++) { if (s[i] == '(') c++; else c--; if (c <= 0 && i != n - 1) bad = 1; } if (!bad && c == 0) cout << s; else cout << ":("; } }
### Prompt Please provide a cpp coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(void) { { ios_base::sync_with_stdio(false); cin.tie(NULL); }; int n, i; cin >> n; string s; cin >> s; if (n & 1) cout << ":("; else { int a = n / 2, b = n / 2, c = 0, bad = 0; for (i = 0; i < n; i++) { if (s[i] == '(') a--; if (s[i] == ')') b--; } for (i = 0; i < n; i++) { if (a <= 0) break; if (s[i] == '?') { s[i] = '('; a--; } } for (i = 0; i < n; i++) { if (b <= 0) break; if (s[i] == '?') { s[i] = ')'; b--; } } for (i = 0; i < n; i++) { if (s[i] == '(') c++; else c--; if (c <= 0 && i != n - 1) bad = 1; } if (!bad && c == 0) cout << s; else cout << ":("; } } ```
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; char str[N]; int l, r, n; int main() { scanf("%d %s", &n, str); if (n < 2 || n % 2 != 0) { printf(":(\n"); return 0; } for (int i = 0; i < n; i += 1) { if (str[i] == '(') l += 1; else if (str[i] == ')') r += 1; if (l > n / 2 || r > n / 2) { printf(":(\n"); return 0; } } for (int i = 0; i < n; i += 1) { if (str[i] == '?') { if (l < n / 2) { str[i] = '('; l += 1; } else { str[i] = ')'; r += 1; } } } if (r > n / 2) { printf(":(\n"); return 0; } l = r = 0; for (int i = 0; i < n; i += 1) { if (str[i] == '(') l += 1; else if (str[i] == ')') --l; if (l < 0 || (l == 0 && i != n - 1)) { printf(":(\n"); return 0; } } if (l != 0) printf(":(\n"); else printf("%s\n", str); return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; char str[N]; int l, r, n; int main() { scanf("%d %s", &n, str); if (n < 2 || n % 2 != 0) { printf(":(\n"); return 0; } for (int i = 0; i < n; i += 1) { if (str[i] == '(') l += 1; else if (str[i] == ')') r += 1; if (l > n / 2 || r > n / 2) { printf(":(\n"); return 0; } } for (int i = 0; i < n; i += 1) { if (str[i] == '?') { if (l < n / 2) { str[i] = '('; l += 1; } else { str[i] = ')'; r += 1; } } } if (r > n / 2) { printf(":(\n"); return 0; } l = r = 0; for (int i = 0; i < n; i += 1) { if (str[i] == '(') l += 1; else if (str[i] == ')') --l; if (l < 0 || (l == 0 && i != n - 1)) { printf(":(\n"); return 0; } } if (l != 0) printf(":(\n"); else printf("%s\n", str); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n % 2 != 0 || s[0] == ')' || s[n - 1] == '(') { cout << ":("; return 0; } int a = 0, b = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') a++; if (s[i] == ')') b++; } n /= 2; a = n - a; b = n - b; int i = 0; n *= 2; for (int i = 0; i < n; i++) { if (s[i] == '?' && a > 0) { s[i] = '('; a--; } } for (int i = 0; i < n; i++) { if (s[i] == '?' && b > 0) { s[i] = ')'; b--; } } a = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') a++; else a--; if (a == 0 && i != (n - 1)) { cout << ":("; return 0; } } if (a != 0) { cout << ":("; return 0; } cout << s; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n % 2 != 0 || s[0] == ')' || s[n - 1] == '(') { cout << ":("; return 0; } int a = 0, b = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') a++; if (s[i] == ')') b++; } n /= 2; a = n - a; b = n - b; int i = 0; n *= 2; for (int i = 0; i < n; i++) { if (s[i] == '?' && a > 0) { s[i] = '('; a--; } } for (int i = 0; i < n; i++) { if (s[i] == '?' && b > 0) { s[i] = ')'; b--; } } a = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') a++; else a--; if (a == 0 && i != (n - 1)) { cout << ":("; return 0; } } if (a != 0) { cout << ":("; return 0; } cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> void out(T x) { cout << x << endl; exit(0); } bool valid(string s) { int state = 0; int n = s.length(); for (int i = 0; i < n; i++) { char c = s[i]; if (c == '(') state++; if (c == '?') return false; if (c == ')') state--; if (state <= 0 && i + 1 < n) return false; } return state == 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; string s; string fail = ":("; cin >> n >> s; if (n % 2) out(fail); int n1 = 0, n2 = 0; for (auto c : s) { if (c == '(') n1++; if (c == ')') n2++; } if (n1 > n / 2 || n2 > n / 2) out(fail); n1 = n / 2 - n1; for (auto &c : s) { if (c != '?') continue; if (n1 > 0) { c = '('; --n1; } else { c = ')'; } } if (valid(s)) out(s); cout << ":(" << endl; return 0; }
### Prompt In CPP, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> void out(T x) { cout << x << endl; exit(0); } bool valid(string s) { int state = 0; int n = s.length(); for (int i = 0; i < n; i++) { char c = s[i]; if (c == '(') state++; if (c == '?') return false; if (c == ')') state--; if (state <= 0 && i + 1 < n) return false; } return state == 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; string s; string fail = ":("; cin >> n >> s; if (n % 2) out(fail); int n1 = 0, n2 = 0; for (auto c : s) { if (c == '(') n1++; if (c == ')') n2++; } if (n1 > n / 2 || n2 > n / 2) out(fail); n1 = n / 2 - n1; for (auto &c : s) { if (c != '?') continue; if (n1 > 0) { c = '('; --n1; } else { c = ')'; } } if (valid(s)) out(s); cout << ":(" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve1() { long long n; cin >> n; if (n % 2 == 1) { cout << ":("; return; } string s; cin >> s; long long m = 0, q = 0; for (auto i : s) { if (i == '(') m++; else if (i == ')') m--; else q++; } if (abs(m) > q) { cout << ":("; return; } q -= abs(m); if (m > 0) for (long long i = n - 1; i >= 0; i--) { if (s[i] == '?') s[i] = ')', m--; if (!m) break; } if (m < 0) for (long long i = 0; i < n; i++) { if (s[i] == '?') s[i] = '(', m++; if (!m) break; } q /= 2; if (q) for (auto &i : s) { if (i == '?') { if (q) i = '(', q--; else i = ')'; } } stack<char> st; long long f = 0; for (auto i : s) { if (i == ')') { if (!st.empty() && st.top() == '(') st.pop(); else { cout << ":("; return; } } else st.push('('); if (st.empty()) f++; } if (st.empty() && f == 1) cout << s; else { cout << ":("; return; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) solve1(); }
### Prompt Please provide a Cpp coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve1() { long long n; cin >> n; if (n % 2 == 1) { cout << ":("; return; } string s; cin >> s; long long m = 0, q = 0; for (auto i : s) { if (i == '(') m++; else if (i == ')') m--; else q++; } if (abs(m) > q) { cout << ":("; return; } q -= abs(m); if (m > 0) for (long long i = n - 1; i >= 0; i--) { if (s[i] == '?') s[i] = ')', m--; if (!m) break; } if (m < 0) for (long long i = 0; i < n; i++) { if (s[i] == '?') s[i] = '(', m++; if (!m) break; } q /= 2; if (q) for (auto &i : s) { if (i == '?') { if (q) i = '(', q--; else i = ')'; } } stack<char> st; long long f = 0; for (auto i : s) { if (i == ')') { if (!st.empty() && st.top() == '(') st.pop(); else { cout << ":("; return; } } else st.push('('); if (st.empty()) f++; } if (st.empty() && f == 1) cout << s; else { cout << ":("; return; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) solve1(); } ```
#include <bits/stdc++.h> using namespace std; const long long SuperINF = 1e9; const long long MAX_SIZE = 1e6, MOD = 1e9 + 7, INF = 1e10; const double eps = 1e-6; using namespace std; void files() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int n; string s, ans; long long vopros, bal0, bal1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; for (int i = 0; i < s.size(); i++) { if (vopros + bal0 < bal1) { cout << ":("; return 0; } if (s[i] == '?') vopros++; if (s[i] == '(') bal0++; if (s[i] == ')') bal1++; } if (bal0 > n / 2) { cout << ":("; return 0; } if (vopros + bal0 < bal1) { cout << ":("; return 0; } if (n % 2 == 1) { cout << ":("; return 0; } long long balance = (n / 2) - bal0; for (int i = 0; i < s.size(); i++) { if (balance == 0) { if (s[i] == '?') s[i] = ')'; continue; } if (s[i] == '?') { balance--; s[i] = '('; } } bal0 = 0, bal1 = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') bal0++; if (s[i] == ')') bal1++; if (i == s.size() - 1) break; if (bal0 == bal1) { cout << ":("; return 0; } } cout << s; }
### Prompt Generate a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long SuperINF = 1e9; const long long MAX_SIZE = 1e6, MOD = 1e9 + 7, INF = 1e10; const double eps = 1e-6; using namespace std; void files() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int n; string s, ans; long long vopros, bal0, bal1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; for (int i = 0; i < s.size(); i++) { if (vopros + bal0 < bal1) { cout << ":("; return 0; } if (s[i] == '?') vopros++; if (s[i] == '(') bal0++; if (s[i] == ')') bal1++; } if (bal0 > n / 2) { cout << ":("; return 0; } if (vopros + bal0 < bal1) { cout << ":("; return 0; } if (n % 2 == 1) { cout << ":("; return 0; } long long balance = (n / 2) - bal0; for (int i = 0; i < s.size(); i++) { if (balance == 0) { if (s[i] == '?') s[i] = ')'; continue; } if (s[i] == '?') { balance--; s[i] = '('; } } bal0 = 0, bal1 = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') bal0++; if (s[i] == ')') bal1++; if (i == s.size() - 1) break; if (bal0 == bal1) { cout << ":("; return 0; } } cout << s; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; string a; cin >> n >> a; int cl = 0, q = 0, rr = 0; for (int i = n - 1; i >= 0; i--) { if (a[i] == ')') cl++; else if (a[i] == '(') cl--; } for (int i = 0; i < n; i++) { if (a[i] == '(') rr++; else if (a[i] == ')') rr--; } for (int i = n - 1; i >= 0; i--) { if (rr <= 0) break; if (a[i] == '?') { a[i] = '#'; rr--; } } for (int i = 0; i < n; i++) { if (cl <= 0) break; if (a[i] == '?') a[i] = '(', cl--; } for (int i = 0; i < n; i++) { if (a[i] == '#') { a[i] = ')'; } } for (int i = 0; i < n; i++) { if (a[i] == '?') q++; } if (q % 2) return cout << ":(", 0; int l = q / 2; for (int i = 0; i < n; i++) { if (!l) break; if (a[i] == '?') { a[i] = '('; l--; } } l = q / 2; for (int i = 0; i < n; i++) { if (!l) break; if (a[i] == '?') { a[i] = ')'; l--; } } int ctr = 1; if (a[0] == ')') return cout << ":(", 0; for (int i = 1; i < n; i++) { if (a[i] == '(') ctr++; else ctr--; if (!ctr && i != n - 1) return cout << ":(", 0; } if (ctr) return cout << ":(", 0; cout << a; return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; string a; cin >> n >> a; int cl = 0, q = 0, rr = 0; for (int i = n - 1; i >= 0; i--) { if (a[i] == ')') cl++; else if (a[i] == '(') cl--; } for (int i = 0; i < n; i++) { if (a[i] == '(') rr++; else if (a[i] == ')') rr--; } for (int i = n - 1; i >= 0; i--) { if (rr <= 0) break; if (a[i] == '?') { a[i] = '#'; rr--; } } for (int i = 0; i < n; i++) { if (cl <= 0) break; if (a[i] == '?') a[i] = '(', cl--; } for (int i = 0; i < n; i++) { if (a[i] == '#') { a[i] = ')'; } } for (int i = 0; i < n; i++) { if (a[i] == '?') q++; } if (q % 2) return cout << ":(", 0; int l = q / 2; for (int i = 0; i < n; i++) { if (!l) break; if (a[i] == '?') { a[i] = '('; l--; } } l = q / 2; for (int i = 0; i < n; i++) { if (!l) break; if (a[i] == '?') { a[i] = ')'; l--; } } int ctr = 1; if (a[0] == ')') return cout << ":(", 0; for (int i = 1; i < n; i++) { if (a[i] == '(') ctr++; else ctr--; if (!ctr && i != n - 1) return cout << ":(", 0; } if (ctr) return cout << ":(", 0; cout << a; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long i, j, k, l; long long x, y, z; long long n; cin >> n; string s; cin >> s; bool flag = 1; if (n % 2 != 0) flag = 0; if (flag == 1) { if (((s[0] == '(' || s[0] == '?') && (s[n - 1] == ')' || s[n - 1] == '?'))) s[0] = '(', s[n - 1] = ')'; else flag = 0; if (n > 2) { if (((s[1] == '(' || s[1] == '?') && (s[n - 2] == ')' || s[n - 2] == '?'))) s[1] = '(', s[n - 2] = ')'; else flag = 0; int count = 0, left = 0, right = 0; for (i = 1; i < n - 1; i++) { if (s[i] == '(') left++; if (s[i] == ')') right++; if (s[i] == '?') count++; if (right > left + count) { flag = 0; break; } } count = 0, left = 0, right = 0; for (i = n - 2; i > 0; i--) { if (s[i] == '(') left++; if (s[i] == ')') right++; if (s[i] == '?') count++; if (left > right + count) { flag = 0; break; } } left--; right--; int d = n - 4; d /= 2; d -= left; if (flag == 1) { for (i = 2; i < n - 2; i++) { if (s[i] == '?') { if (d > 0) { s[i] = '('; d--; } else s[i] = ')'; } } } } } if (flag == 1) cout << s; else cout << ":("; return 0; }
### Prompt In Cpp, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long i, j, k, l; long long x, y, z; long long n; cin >> n; string s; cin >> s; bool flag = 1; if (n % 2 != 0) flag = 0; if (flag == 1) { if (((s[0] == '(' || s[0] == '?') && (s[n - 1] == ')' || s[n - 1] == '?'))) s[0] = '(', s[n - 1] = ')'; else flag = 0; if (n > 2) { if (((s[1] == '(' || s[1] == '?') && (s[n - 2] == ')' || s[n - 2] == '?'))) s[1] = '(', s[n - 2] = ')'; else flag = 0; int count = 0, left = 0, right = 0; for (i = 1; i < n - 1; i++) { if (s[i] == '(') left++; if (s[i] == ')') right++; if (s[i] == '?') count++; if (right > left + count) { flag = 0; break; } } count = 0, left = 0, right = 0; for (i = n - 2; i > 0; i--) { if (s[i] == '(') left++; if (s[i] == ')') right++; if (s[i] == '?') count++; if (left > right + count) { flag = 0; break; } } left--; right--; int d = n - 4; d /= 2; d -= left; if (flag == 1) { for (i = 2; i < n - 2; i++) { if (s[i] == '?') { if (d > 0) { s[i] = '('; d--; } else s[i] = ')'; } } } } } if (flag == 1) cout << s; else cout << ":("; return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int MAX = 300005; long long modexp(long long x, long long n) { if (n == 0) return 1LL; if (n % 2 == 0) { long long y = modexp(x, n / 2) % mod; return (y * y) % mod; } return (x * modexp(x, n - 1) % mod) % mod; } long long powr(long long x, long long n) { long long ans = 1; for (int i = 1; i <= n; i++) ans = ans * x; return ans; } int a[MAX]; int b[MAX]; int main() { int n; scanf("%d", &n); string s; cin >> s; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; if (n % 2) return 0 * printf(":(\n"); int ques = 0; for (int i = 0; i < n; i++) { if (s[i] == '?') ques++; } if (ques == 0) { int cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt++; else cnt--; if (cnt < 0) return 0 * printf(":(\n"); if (cnt == 0 && (i != n - 1)) return 0 * printf(":(\n"); } if (cnt > 0) printf(":(\n"); else cout << s << endl; return 0; } for (int i = 0; i < n; i++) { if (s[i] == ')') a[i] = -1; else a[i] = 1; } for (int i = 0; i < n; i++) { if (s[i] == '(') b[i] = 1; else b[i] = -1; } for (int i = n - 2; i >= 0; i--) { b[i] += b[i + 1]; } for (int i = 1; i < n; i++) { a[i] += a[i - 1]; } int flag = 0; for (int i = 0; i < n; i++) { if (a[i] < 0 || b[i] > 0) { flag = 1; break; } if (a[i] == 0) { if (i != n - 1) { flag = 1; break; } } if (b[i] == 0) { if (i != 0) { flag = 1; break; } } } if (flag == 1) { printf(":(\n"); return 0; } int cnt = 1; deque<int> st; for (int i = 1; i < n - 1; i++) { if (s[i] != ')') { if (s[i] == '(') st.push_back(i); else st.push_front(i); } else { if (st.empty() == true) { return 0 * printf(":(\n"); } int idx = st.back(); st.pop_back(); if (s[idx] == '?') s[idx] = '('; } } while (st.size() > 0) { int x = st.back(); int y = st.front(); st.pop_back(); st.pop_front(); if (s[x] == '(') { if (x > y) return 0 * printf(":(\n"); s[y] = ')'; } else { if (x < y) { s[x] = '('; s[y] = ')'; } else { s[x] = ')'; s[y] = '('; } } } cout << s << endl; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int MAX = 300005; long long modexp(long long x, long long n) { if (n == 0) return 1LL; if (n % 2 == 0) { long long y = modexp(x, n / 2) % mod; return (y * y) % mod; } return (x * modexp(x, n - 1) % mod) % mod; } long long powr(long long x, long long n) { long long ans = 1; for (int i = 1; i <= n; i++) ans = ans * x; return ans; } int a[MAX]; int b[MAX]; int main() { int n; scanf("%d", &n); string s; cin >> s; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; if (n % 2) return 0 * printf(":(\n"); int ques = 0; for (int i = 0; i < n; i++) { if (s[i] == '?') ques++; } if (ques == 0) { int cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt++; else cnt--; if (cnt < 0) return 0 * printf(":(\n"); if (cnt == 0 && (i != n - 1)) return 0 * printf(":(\n"); } if (cnt > 0) printf(":(\n"); else cout << s << endl; return 0; } for (int i = 0; i < n; i++) { if (s[i] == ')') a[i] = -1; else a[i] = 1; } for (int i = 0; i < n; i++) { if (s[i] == '(') b[i] = 1; else b[i] = -1; } for (int i = n - 2; i >= 0; i--) { b[i] += b[i + 1]; } for (int i = 1; i < n; i++) { a[i] += a[i - 1]; } int flag = 0; for (int i = 0; i < n; i++) { if (a[i] < 0 || b[i] > 0) { flag = 1; break; } if (a[i] == 0) { if (i != n - 1) { flag = 1; break; } } if (b[i] == 0) { if (i != 0) { flag = 1; break; } } } if (flag == 1) { printf(":(\n"); return 0; } int cnt = 1; deque<int> st; for (int i = 1; i < n - 1; i++) { if (s[i] != ')') { if (s[i] == '(') st.push_back(i); else st.push_front(i); } else { if (st.empty() == true) { return 0 * printf(":(\n"); } int idx = st.back(); st.pop_back(); if (s[idx] == '?') s[idx] = '('; } } while (st.size() > 0) { int x = st.back(); int y = st.front(); st.pop_back(); st.pop_front(); if (s[x] == '(') { if (x > y) return 0 * printf(":(\n"); s[y] = ')'; } else { if (x < y) { s[x] = '('; s[y] = ')'; } else { s[x] = ')'; s[y] = '('; } } } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; string s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); clock_t ck; ck = clock(); cin >> n >> s; int op = n / 2; int cl = n / 2; for (int i = 0; i < n; i++) { op -= (s[i] == '('); cl -= (s[i] == ')'); } int cnt = 0; bool good = 1; for (int i = 0; i < n && good; i++) { if (cnt <= 0 && i != 0 && i != n - 1) good = 0; if (s[i] != '?') { cnt += (s[i] == '(' ? 1 : -1); continue; } if (cnt + 1 != 0 && op > 0) op--, cnt++, s[i] = '('; else cl--, cnt--, s[i] = ')'; } cout << (cnt == 0 && good ? s : ":(") << '\n'; ck = clock() - ck; cerr << "It took " << 1.0 * ck / CLOCKS_PER_SEC << " sec\n"; return 0; }
### Prompt Develop a solution in CPP to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; string s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); clock_t ck; ck = clock(); cin >> n >> s; int op = n / 2; int cl = n / 2; for (int i = 0; i < n; i++) { op -= (s[i] == '('); cl -= (s[i] == ')'); } int cnt = 0; bool good = 1; for (int i = 0; i < n && good; i++) { if (cnt <= 0 && i != 0 && i != n - 1) good = 0; if (s[i] != '?') { cnt += (s[i] == '(' ? 1 : -1); continue; } if (cnt + 1 != 0 && op > 0) op--, cnt++, s[i] = '('; else cl--, cnt--, s[i] = ')'; } cout << (cnt == 0 && good ? s : ":(") << '\n'; ck = clock() - ck; cerr << "It took " << 1.0 * ck / CLOCKS_PER_SEC << " sec\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1E6 + 10; char str[N]; int dis[N]; int main() { int t; while (cin >> t) { scanf("%s", str); int len = strlen(str); int k = 0; int f = 0; int l, r; int t = 0; l = 0, r = 0; for (int i = 0; i < len - 1; i++) { if (str[i] == '(') { k++; } else if (str[i] == ')') { if (k <= 1) { str[dis[l++]] = '('; } else k--; } else if (str[i] == '?') { dis[r++] = i; } } if (str[len - 1] == '(') { printf(":(\n"); continue; } int re = (r - l + 1); int ll = (re - k) / 2; int rr = re - (re - k) / 2; for (int i = 0; i < len; i++) { if (str[i] == '?' && ll) { str[i] = '('; ll--; } else if (str[i] == '?' && rr) { str[i] = ')'; rr--; } } k = 0; for (int i = 0; i < len - 1; i++) { if (str[i] == '(') { k++; } else { if (k > 1) k--; else { f = 1; break; } } } if (str[len - 1] == ')') k--; if (k == 0 && f == 0) { printf("%s\n", str); } else printf(":(\n"); } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1E6 + 10; char str[N]; int dis[N]; int main() { int t; while (cin >> t) { scanf("%s", str); int len = strlen(str); int k = 0; int f = 0; int l, r; int t = 0; l = 0, r = 0; for (int i = 0; i < len - 1; i++) { if (str[i] == '(') { k++; } else if (str[i] == ')') { if (k <= 1) { str[dis[l++]] = '('; } else k--; } else if (str[i] == '?') { dis[r++] = i; } } if (str[len - 1] == '(') { printf(":(\n"); continue; } int re = (r - l + 1); int ll = (re - k) / 2; int rr = re - (re - k) / 2; for (int i = 0; i < len; i++) { if (str[i] == '?' && ll) { str[i] = '('; ll--; } else if (str[i] == '?' && rr) { str[i] = ')'; rr--; } } k = 0; for (int i = 0; i < len - 1; i++) { if (str[i] == '(') { k++; } else { if (k > 1) k--; else { f = 1; break; } } } if (str[len - 1] == ')') k--; if (k == 0 && f == 0) { printf("%s\n", str); } else printf(":(\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int N; cin >> N; string s; cin >> s; if (N & 1) cout << ":("; else { int flag = 0, g = 0, l = 0, r = 0; for (int i = 0; i < N; i++) { if (s[i] == '(') l++; else if (s[i] == ')') r++; } int h = 0, j = 0; for (int i = 0; i < N; i++) { if (s[i] == '?') { if (l < N / 2) s[i] = '(', l++, h++; else s[i] = ')', r++, j++; } else if (s[i] == '(') h++; else j++; if (j > h || (j == h && i < N - 1)) flag = 1; } if (l == r && !flag) cout << s; else cout << ":("; } }
### Prompt Construct a cpp code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int N; cin >> N; string s; cin >> s; if (N & 1) cout << ":("; else { int flag = 0, g = 0, l = 0, r = 0; for (int i = 0; i < N; i++) { if (s[i] == '(') l++; else if (s[i] == ')') r++; } int h = 0, j = 0; for (int i = 0; i < N; i++) { if (s[i] == '?') { if (l < N / 2) s[i] = '(', l++, h++; else s[i] = ')', r++, j++; } else if (s[i] == '(') h++; else j++; if (j > h || (j == h && i < N - 1)) flag = 1; } if (l == r && !flag) cout << s; else cout << ":("; } } ```
#include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int sl; std::cin >> sl; std::string s; std::cin >> s; bool ans = true; ; int leftamount = 0; int rightamount = 0; int nowleft = 0; int nowright = 0; if (sl % 2 == 1 || s[0] == ')' || s[sl - 1] == '(') { ans = false; } else { for (int i = 0; i < sl; i++) { if (s[i] == '(') { leftamount++; } else if (s[i] == ')') { rightamount++; } } for (int i = 0; i < sl; i++) { if (i != 0 && nowleft == nowright || nowright > nowleft) { ans = false; break; } if (s[i] == '(') { nowleft++; leftamount--; continue; } else if (s[i] == ')') { nowright++; rightamount--; continue; } if (leftamount + nowleft < sl / 2) { s[i] = '('; nowleft++; } else { s[i] = ')'; nowright++; } } } if (nowleft != nowright) { ans = false; } std::cout << (ans ? s : ":(") << "\n"; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int sl; std::cin >> sl; std::string s; std::cin >> s; bool ans = true; ; int leftamount = 0; int rightamount = 0; int nowleft = 0; int nowright = 0; if (sl % 2 == 1 || s[0] == ')' || s[sl - 1] == '(') { ans = false; } else { for (int i = 0; i < sl; i++) { if (s[i] == '(') { leftamount++; } else if (s[i] == ')') { rightamount++; } } for (int i = 0; i < sl; i++) { if (i != 0 && nowleft == nowright || nowright > nowleft) { ans = false; break; } if (s[i] == '(') { nowleft++; leftamount--; continue; } else if (s[i] == ')') { nowright++; rightamount--; continue; } if (leftamount + nowleft < sl / 2) { s[i] = '('; nowleft++; } else { s[i] = ')'; nowright++; } } } if (nowleft != nowright) { ans = false; } std::cout << (ans ? s : ":(") << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, m, k, t, caseno, h; char s[300005]; int main() { long long i, j; scanf("%lld", &n); scanf("%s", &s); if (n & 1) { puts(":("); return 0; } long long left = 0, right = 0; for (i = 0; i <= n - 1; i++) { if (s[i] == '(') left++; if (s[i] == ')') right++; } if (left > n / 2 || right > n / 2) { puts(":("); return 0; } long long l = 0, r = 0; long long left_baki = n / 2 - left; l = 0, r = 0; for (i = 0; i <= n - 1; i++) { if (s[i] == '?') { if (left_baki) { s[i] = '('; l++; left_baki--; } else { r++; s[i] = ')'; } } else { if (s[i] == '(') l++; else r++; } if (i != n - 1 && l == r) { puts(":("); return 0; } if (r > l) { puts(":("); return 0; } } for (i = 0; i <= n - 1; i++) { printf("%c", s[i]); } puts(""); return 0; }
### Prompt Create a solution in Cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, m, k, t, caseno, h; char s[300005]; int main() { long long i, j; scanf("%lld", &n); scanf("%s", &s); if (n & 1) { puts(":("); return 0; } long long left = 0, right = 0; for (i = 0; i <= n - 1; i++) { if (s[i] == '(') left++; if (s[i] == ')') right++; } if (left > n / 2 || right > n / 2) { puts(":("); return 0; } long long l = 0, r = 0; long long left_baki = n / 2 - left; l = 0, r = 0; for (i = 0; i <= n - 1; i++) { if (s[i] == '?') { if (left_baki) { s[i] = '('; l++; left_baki--; } else { r++; s[i] = ')'; } } else { if (s[i] == '(') l++; else r++; } if (i != n - 1 && l == r) { puts(":("); return 0; } if (r > l) { puts(":("); return 0; } } for (i = 0; i <= n - 1; i++) { printf("%c", s[i]); } puts(""); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(0); int znaki; cin >> znaki; vector<char> wektor(znaki); int lewe = 0; int prawe = 0; int pytajniki = 0; for (int i = 0; i < znaki; i++) { cin >> wektor[i]; if (wektor[i] == '(') lewe += 1; if (wektor[i] == ')') prawe += 1; if (wektor[i] == '?') pytajniki += 1; } if (znaki % 2 == 1) { cout << ":(\n"; return 0; } int lewe_do_postawienia = (znaki / 2) - lewe; int prawe_do_postawienia = (znaki / 2) - prawe; int obecna_wartosc = 0; for (int i = 0; i < znaki - 1; i++) { if (wektor[i] == '(') { obecna_wartosc += 1; } else if (wektor[i] == ')') { obecna_wartosc -= 1; if (obecna_wartosc <= 0) { cout << ":(\n"; return 0; } } else { if (lewe_do_postawienia > 0) { wektor[i] = '('; lewe_do_postawienia -= 1; obecna_wartosc += 1; } else { wektor[i] = ')'; prawe_do_postawienia -= 1; obecna_wartosc -= 1; if (obecna_wartosc <= 0) { cout << ":(\n"; return 0; } } } } if (obecna_wartosc == 1 && (wektor[znaki - 1] == ')' || wektor[znaki - 1] == '?')) { wektor[znaki - 1] = ')'; for (int i = 0; i < znaki; i++) { cout << wektor[i]; } cout << "\n"; } else { cout << ":(\n"; return 0; } return 0; }
### Prompt Develop a solution in CPP to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(0); int znaki; cin >> znaki; vector<char> wektor(znaki); int lewe = 0; int prawe = 0; int pytajniki = 0; for (int i = 0; i < znaki; i++) { cin >> wektor[i]; if (wektor[i] == '(') lewe += 1; if (wektor[i] == ')') prawe += 1; if (wektor[i] == '?') pytajniki += 1; } if (znaki % 2 == 1) { cout << ":(\n"; return 0; } int lewe_do_postawienia = (znaki / 2) - lewe; int prawe_do_postawienia = (znaki / 2) - prawe; int obecna_wartosc = 0; for (int i = 0; i < znaki - 1; i++) { if (wektor[i] == '(') { obecna_wartosc += 1; } else if (wektor[i] == ')') { obecna_wartosc -= 1; if (obecna_wartosc <= 0) { cout << ":(\n"; return 0; } } else { if (lewe_do_postawienia > 0) { wektor[i] = '('; lewe_do_postawienia -= 1; obecna_wartosc += 1; } else { wektor[i] = ')'; prawe_do_postawienia -= 1; obecna_wartosc -= 1; if (obecna_wartosc <= 0) { cout << ":(\n"; return 0; } } } } if (obecna_wartosc == 1 && (wektor[znaki - 1] == ')' || wektor[znaki - 1] == '?')) { wektor[znaki - 1] = ')'; for (int i = 0; i < znaki; i++) { cout << wektor[i]; } cout << "\n"; } else { cout << ":(\n"; return 0; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; stack<pair<char, int> > sta, sta2; string ans; int main() { int n; string a; cin >> n >> a; ans = a; if (n % 2 == 1 || a[0] == ')' || a[n - 1] == '(') { cout << ":(" << endl; return 0; } ans[0] = '('; ans[n - 1] = ')'; for (int i = 1; i < n / 2; i++) { if (a[i] == '(') { sta.push(make_pair('(', i)); ans[i] = '('; } else if (a[i] == ')') { if (sta.empty()) { cout << ":(" << endl; return 0; } else { if (sta.top().first == '?') { ans[sta.top().second] = '('; } sta.pop(); ans[i] = ')'; } } else if (a[i] == '?') { if (sta.empty()) { sta.push(make_pair('(', i)); ans[i] = '('; } else { sta.push(make_pair('?', i)); ans[i] = '?'; } } } for (int i = n - 2; i >= n / 2; i--) { if (a[i] == ')') { sta2.push(make_pair(')', i)); ans[i] = ')'; } else if (a[i] == '(') { if (sta2.empty()) { cout << ":(" << endl; return 0; } else { if (sta2.top().first == '?') { ans[sta2.top().second] = ')'; } sta2.pop(); ans[i] = '('; } } else if (a[i] == '?') { if (sta2.empty()) { sta2.push(make_pair(')', i)); ans[i] = ')'; } else { sta2.push(make_pair('?', i)); ans[i] = '?'; } } } if (sta.size() > sta2.size()) { int m = (sta.size() - sta2.size()) / 2; int p = 0, q = 0; while (p < m) { if (sta.top().first == '(') { sta.pop(); q++; if (q > p + sta2.size()) { cout << ":(" << endl; return 0; } continue; } ans[sta.top().second] = ')'; sta.pop(); p++; } } else if (sta.size() < sta2.size()) { int m = (sta2.size() - sta.size()) / 2; int p = 0, q = 0; while (p < m) { if (sta2.top().first == ')') { sta2.pop(); q++; if (q > p + sta.size()) { cout << ":(" << endl; return 0; } continue; } ans[sta2.top().second] = '('; sta2.pop(); p++; } } while (sta.size()) { ans[sta.top().second] = '('; sta.pop(); } while (sta2.size()) { ans[sta2.top().second] = ')'; sta2.pop(); } cout << ans; return 0; }
### Prompt Develop a solution in CPP to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; stack<pair<char, int> > sta, sta2; string ans; int main() { int n; string a; cin >> n >> a; ans = a; if (n % 2 == 1 || a[0] == ')' || a[n - 1] == '(') { cout << ":(" << endl; return 0; } ans[0] = '('; ans[n - 1] = ')'; for (int i = 1; i < n / 2; i++) { if (a[i] == '(') { sta.push(make_pair('(', i)); ans[i] = '('; } else if (a[i] == ')') { if (sta.empty()) { cout << ":(" << endl; return 0; } else { if (sta.top().first == '?') { ans[sta.top().second] = '('; } sta.pop(); ans[i] = ')'; } } else if (a[i] == '?') { if (sta.empty()) { sta.push(make_pair('(', i)); ans[i] = '('; } else { sta.push(make_pair('?', i)); ans[i] = '?'; } } } for (int i = n - 2; i >= n / 2; i--) { if (a[i] == ')') { sta2.push(make_pair(')', i)); ans[i] = ')'; } else if (a[i] == '(') { if (sta2.empty()) { cout << ":(" << endl; return 0; } else { if (sta2.top().first == '?') { ans[sta2.top().second] = ')'; } sta2.pop(); ans[i] = '('; } } else if (a[i] == '?') { if (sta2.empty()) { sta2.push(make_pair(')', i)); ans[i] = ')'; } else { sta2.push(make_pair('?', i)); ans[i] = '?'; } } } if (sta.size() > sta2.size()) { int m = (sta.size() - sta2.size()) / 2; int p = 0, q = 0; while (p < m) { if (sta.top().first == '(') { sta.pop(); q++; if (q > p + sta2.size()) { cout << ":(" << endl; return 0; } continue; } ans[sta.top().second] = ')'; sta.pop(); p++; } } else if (sta.size() < sta2.size()) { int m = (sta2.size() - sta.size()) / 2; int p = 0, q = 0; while (p < m) { if (sta2.top().first == ')') { sta2.pop(); q++; if (q > p + sta.size()) { cout << ":(" << endl; return 0; } continue; } ans[sta2.top().second] = '('; sta2.pop(); p++; } } while (sta.size()) { ans[sta.top().second] = '('; sta.pop(); } while (sta2.size()) { ans[sta2.top().second] = ')'; sta2.pop(); } cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 3e5; const int oo = 1e9 + 5; int du[] = {-1, 0, 0, 1}; int dv[] = {0, -1, 1, 0}; const long long mod = 1e9 + 7; long long sqr(long long x) { return x * x; } int n, t = 0; string s; char res[N + 5]; int main() { ios_base::sync_with_stdio(false); cin >> n; cin >> s; s = " " + s; if (n % 2 == 1) { cout << ":("; return 0; } int num1 = n / 2, num2 = n / 2; for (int i = 1; i <= n; i++) { if (s[i] == '(') num1--; if (s[i] == ')') num2--; } for (int i = 1; i <= n; i++) { if (s[i] == '(') { res[i] = '('; t++; if ((t == 0 && i != n) || t < 0) { cout << ":("; return 0; }; continue; } if (s[i] == ')') { res[i] = ')'; t--; if ((t == 0 && i != n) || t < 0) { cout << ":("; return 0; }; continue; } if (num1 > 0) { res[i] = '('; num1--; t++; } else { res[i] = ')'; num2--; t--; } if ((t == 0 && i != n) || t < 0) { cout << ":("; return 0; } } if (t != 0) { cout << ":("; return 0; } for (int i = 1; i <= n; i++) cout << res[i]; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 3e5; const int oo = 1e9 + 5; int du[] = {-1, 0, 0, 1}; int dv[] = {0, -1, 1, 0}; const long long mod = 1e9 + 7; long long sqr(long long x) { return x * x; } int n, t = 0; string s; char res[N + 5]; int main() { ios_base::sync_with_stdio(false); cin >> n; cin >> s; s = " " + s; if (n % 2 == 1) { cout << ":("; return 0; } int num1 = n / 2, num2 = n / 2; for (int i = 1; i <= n; i++) { if (s[i] == '(') num1--; if (s[i] == ')') num2--; } for (int i = 1; i <= n; i++) { if (s[i] == '(') { res[i] = '('; t++; if ((t == 0 && i != n) || t < 0) { cout << ":("; return 0; }; continue; } if (s[i] == ')') { res[i] = ')'; t--; if ((t == 0 && i != n) || t < 0) { cout << ":("; return 0; }; continue; } if (num1 > 0) { res[i] = '('; num1--; t++; } else { res[i] = ')'; num2--; t--; } if ((t == 0 && i != n) || t < 0) { cout << ":("; return 0; } } if (t != 0) { cout << ":("; return 0; } for (int i = 1; i <= n; i++) cout << res[i]; return 0; } ```
#include <bits/stdc++.h> using namespace std; string s; int main() { int n, i, a, b, x, y, p, q; cin >> n >> s; if (n % 2 == 1) { cout << ":("; return 0; } if (s[0] == ')' || s[n - 1] == '(') { cout << ":("; return 0; } a = b = n / 2 - 1; for (i = 1; i < n - 1; i++) { if (s[i] == '(') a--; if (s[i] == ')') b--; } if (a < 0 || b < 0) { cout << ":("; return 0; } x = y = p = q = 0; for (i = 1; i < n - 1; i++) { if (s[i] == '(') x++; if (s[i] == ')') y++; if (s[i] == '?') { if (p < a) { p++; x++; s[i] = '('; } else if (q < b) { q++; y++; s[i] = ')'; } } if (s[i] == '?') { cout << ":("; return 0; } if (x < y) { cout << ":("; return 0; } } s[0] = '('; s[n - 1] = ')'; cout << s; return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s; int main() { int n, i, a, b, x, y, p, q; cin >> n >> s; if (n % 2 == 1) { cout << ":("; return 0; } if (s[0] == ')' || s[n - 1] == '(') { cout << ":("; return 0; } a = b = n / 2 - 1; for (i = 1; i < n - 1; i++) { if (s[i] == '(') a--; if (s[i] == ')') b--; } if (a < 0 || b < 0) { cout << ":("; return 0; } x = y = p = q = 0; for (i = 1; i < n - 1; i++) { if (s[i] == '(') x++; if (s[i] == ')') y++; if (s[i] == '?') { if (p < a) { p++; x++; s[i] = '('; } else if (q < b) { q++; y++; s[i] = ')'; } } if (s[i] == '?') { cout << ":("; return 0; } if (x < y) { cout << ":("; return 0; } } s[0] = '('; s[n - 1] = ')'; cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; string no = ":("; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long n, i, j, k; cin >> n; string s; cin >> s; if (n % 2) { cout << no << '\n'; return 0; } long long o, c; o = c = 0; for (i = 0; i < n; i++) { if (s[i] == ')') c++; else if (s[i] == '(') o++; } if (o > n / 2 || c > n / 2) { cout << no << '\n'; return 0; } o = n / 2 - o; c = n / 2 - c; for (i = 0; i < n; i++) { if (o > 0) { if (s[i] == '?') { s[i] = '('; o--; } } } for (i = n - 1; i >= 0; i--) { if (c > 0) { if (s[i] == '?') { s[i] = ')'; c--; } } } for (i = 0; i < n; i++) { if (s[i] == '?') { cout << no << '\n'; return 0; } } long long b = 0; for (i = 0; i < n; i++) { if (s[i] == '(') b++; else { if (b > 0) b--; else { cout << no << '\n'; return 0; } } if (b == 0 && i != n - 1) { cout << no << '\n'; return 0; } } cout << s << '\n'; }
### Prompt Please provide a CPP coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; string no = ":("; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long n, i, j, k; cin >> n; string s; cin >> s; if (n % 2) { cout << no << '\n'; return 0; } long long o, c; o = c = 0; for (i = 0; i < n; i++) { if (s[i] == ')') c++; else if (s[i] == '(') o++; } if (o > n / 2 || c > n / 2) { cout << no << '\n'; return 0; } o = n / 2 - o; c = n / 2 - c; for (i = 0; i < n; i++) { if (o > 0) { if (s[i] == '?') { s[i] = '('; o--; } } } for (i = n - 1; i >= 0; i--) { if (c > 0) { if (s[i] == '?') { s[i] = ')'; c--; } } } for (i = 0; i < n; i++) { if (s[i] == '?') { cout << no << '\n'; return 0; } } long long b = 0; for (i = 0; i < n; i++) { if (s[i] == '(') b++; else { if (b > 0) b--; else { cout << no << '\n'; return 0; } } if (b == 0 && i != n - 1) { cout << no << '\n'; return 0; } } cout << s << '\n'; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; long long n, m, c, t; string s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int c, t = 0; string s; cin >> c >> s; for (int i = 0; i < c; i++) { if (s[i] == '(') t++; } int a = 0; for (int i = 0; i < c; i++) { if (s[i] == '?') { if (2 * t < c) s[i] = '(', t++; else s[i] = ')'; } if (s[i] == '(') a++; if ((2 * a < i + 1) || (i == c - 1 && 2 * a != i + 1) || (i != c - 1 && 2 * a == i + 1)) return cout << ":(" && 0; } cout << s; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; long long n, m, c, t; string s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int c, t = 0; string s; cin >> c >> s; for (int i = 0; i < c; i++) { if (s[i] == '(') t++; } int a = 0; for (int i = 0; i < c; i++) { if (s[i] == '?') { if (2 * t < c) s[i] = '(', t++; else s[i] = ')'; } if (s[i] == '(') a++; if ((2 * a < i + 1) || (i == c - 1 && 2 * a != i + 1) || (i != c - 1 && 2 * a == i + 1)) return cout << ":(" && 0; } cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; string word; cin >> word; if (N % 2 == 1) { printf(":(\n"); return 0; } int op_l = N / 2, cl_l = N / 2; for (int i = 0; i < word.size(); i++) { if (word[i] == '(') op_l--; if (word[i] == ')') cl_l--; } int open = 0; for (int i = 0; i < word.size(); i++) { if (word[i] == '(') open++; if (word[i] == ')') open--; if (word[i] == '?') { if (op_l > 0) { word[i] = '('; op_l--; open++; } else { word[i] = ')'; cl_l--; open--; } } if (open < 0 || (open == 0 && i < word.size() - 1)) { printf(":(\n"); return 0; } } if (open == 0) cout << word << endl; else cout << ":(\n"; }
### Prompt Create a solution in CPP for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; string word; cin >> word; if (N % 2 == 1) { printf(":(\n"); return 0; } int op_l = N / 2, cl_l = N / 2; for (int i = 0; i < word.size(); i++) { if (word[i] == '(') op_l--; if (word[i] == ')') cl_l--; } int open = 0; for (int i = 0; i < word.size(); i++) { if (word[i] == '(') open++; if (word[i] == ')') open--; if (word[i] == '?') { if (op_l > 0) { word[i] = '('; op_l--; open++; } else { word[i] = ')'; cl_l--; open--; } } if (open < 0 || (open == 0 && i < word.size() - 1)) { printf(":(\n"); return 0; } } if (open == 0) cout << word << endl; else cout << ":(\n"; } ```
#include <bits/stdc++.h> using namespace std; const int N = 300005, M = 1000000007, BIG = 0x3f3f3f3f; int n; string s; void Process() { cin >> n >> s; if (s[0] == ')' || s[n - 1] == '(') { cout << ":(\n"; return; } if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; int x = 0, y = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') x++; else if (s[i] == ')') { y++; if (y > x) { for (int j = i - 1; j; j--) { if (s[j] == '?') { s[j] = '('; x++; break; } } if (y > x) { cout << ":(\n"; return; } } } } x = y = 0; for (int i = n - 2; i; i--) { if (s[i] == '(') { x++; if (x > y) { for (int j = i + 1; j < n - 1; j++) { if (s[j] == '?') { s[j] = ')'; y++; break; } } if (x > y) { cout << ":(\n"; return; } } } else if (s[i] == ')') y++; } x = y = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') x++; else if (s[i] == ')') y++; } for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { if (x > y) { s[i] = ')'; y++; } else { s[i] = '('; x++; } } } if (x == y) cout << s << "\n"; else cout << ":(\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; while (T--) Process(); return 0; }
### Prompt Create a solution in Cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 300005, M = 1000000007, BIG = 0x3f3f3f3f; int n; string s; void Process() { cin >> n >> s; if (s[0] == ')' || s[n - 1] == '(') { cout << ":(\n"; return; } if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; int x = 0, y = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') x++; else if (s[i] == ')') { y++; if (y > x) { for (int j = i - 1; j; j--) { if (s[j] == '?') { s[j] = '('; x++; break; } } if (y > x) { cout << ":(\n"; return; } } } } x = y = 0; for (int i = n - 2; i; i--) { if (s[i] == '(') { x++; if (x > y) { for (int j = i + 1; j < n - 1; j++) { if (s[j] == '?') { s[j] = ')'; y++; break; } } if (x > y) { cout << ":(\n"; return; } } } else if (s[i] == ')') y++; } x = y = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') x++; else if (s[i] == ')') y++; } for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { if (x > y) { s[i] = ')'; y++; } else { s[i] = '('; x++; } } } if (x == y) cout << s << "\n"; else cout << ":(\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; while (T--) Process(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long arr[200005]; long long brr[200005]; int main() { long long i, j, n, m, t, a = 0, b = 0; string s; cin >> n; cin >> s; long long sz = s.size(); for (i = 0; i < n; i++) { if (s[i] == '(') a++; else if (s[i] == ')') b++; } if (n % 2 == 1 || a > n / 2 || b > n / 2) { cout << ":(" << endl; return 0; } a = n / 2 - a; b = n / 2 - b; for (i = 0; i < n; i++) { if (s[i] == '?') { if (a != 0) { s[i] = '('; a--; } else { s[i] = ')'; } } } a = 0; for (i = 0; i < n; i++) { if (s[i] == '(') a++; else if (s[i] == ')') a--; if (a <= 0 && i != n - 1) { cout << ":(" << endl; return 0; } } cout << s << endl; }
### Prompt Develop a solution in Cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long arr[200005]; long long brr[200005]; int main() { long long i, j, n, m, t, a = 0, b = 0; string s; cin >> n; cin >> s; long long sz = s.size(); for (i = 0; i < n; i++) { if (s[i] == '(') a++; else if (s[i] == ')') b++; } if (n % 2 == 1 || a > n / 2 || b > n / 2) { cout << ":(" << endl; return 0; } a = n / 2 - a; b = n / 2 - b; for (i = 0; i < n; i++) { if (s[i] == '?') { if (a != 0) { s[i] = '('; a--; } else { s[i] = ')'; } } } a = 0; for (i = 0; i < n; i++) { if (s[i] == '(') a++; else if (s[i] == ')') a--; if (a <= 0 && i != n - 1) { cout << ":(" << endl; return 0; } } cout << s << endl; } ```
#include <bits/stdc++.h> using namespace std; int n, nr, RL[300005]; string s; int main() { ios::sync_with_stdio(false); cin >> n; cin >> s; if (n % 2 == 1) { cout << ":(\n"; return 0; } for (int i = 0; i < n; i++) if (s[i] == '(') nr++; for (int i = 0; i < n; i++) if (s[i] == '?') { if (nr < n / 2) s[i] = '(', nr++; else s[i] = ')'; } nr = 0; for (int i = n - 1; i >= 0; i--) { RL[i] = RL[i + 1]; if (s[i] == ')') RL[i]++; else RL[i]--; if (nr < 0) { cout << ":(\n"; return 0; } } nr = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') nr++; else nr--; if (nr < 0) { cout << ":(\n"; return 0; } if (nr == 0 && RL[0] - RL[i + 1] == 0 && i != n - 1) { cout << ":(\n"; return 0; } if (i == n - 1 && (nr != 0 || RL[0] - RL[i + 1] != 0)) { cout << ":(\n"; return 0; } } cout << s << '\n'; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, nr, RL[300005]; string s; int main() { ios::sync_with_stdio(false); cin >> n; cin >> s; if (n % 2 == 1) { cout << ":(\n"; return 0; } for (int i = 0; i < n; i++) if (s[i] == '(') nr++; for (int i = 0; i < n; i++) if (s[i] == '?') { if (nr < n / 2) s[i] = '(', nr++; else s[i] = ')'; } nr = 0; for (int i = n - 1; i >= 0; i--) { RL[i] = RL[i + 1]; if (s[i] == ')') RL[i]++; else RL[i]--; if (nr < 0) { cout << ":(\n"; return 0; } } nr = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') nr++; else nr--; if (nr < 0) { cout << ":(\n"; return 0; } if (nr == 0 && RL[0] - RL[i + 1] == 0 && i != n - 1) { cout << ":(\n"; return 0; } if (i == n - 1 && (nr != 0 || RL[0] - RL[i + 1] != 0)) { cout << ":(\n"; return 0; } } cout << s << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; int l = 0, r = 0, q = 0, a, b, c; scanf("%d", &a); cin >> s; a = 0, b = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') l++; else if (s[i] == ')') r++; else q++; } if ((s.size() & 1)) { cout << ":(" << endl; return 0; } if (l > (s.size() / 2) or r > (s.size() / 2)) { cout << ":(" << endl; return 0; } int x = (s.size() / 2) - l, y = (s.size() / 2) - r; int key = 1; for (int i = 0; i < s.size(); i++) { if (a <= b and i != 0) { cout << ":(" << endl; return 0; } if (s[i] == '(') a++; else if (s[i] == ')') b++; else { if (x != 0) { s[i] = '('; x--; a++; } else { s[i] = ')'; y--; b++; } } } if (a != b) { cout << ":(" << endl; } else cout << s << endl; return 0; }
### Prompt Develop a solution in cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; int l = 0, r = 0, q = 0, a, b, c; scanf("%d", &a); cin >> s; a = 0, b = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') l++; else if (s[i] == ')') r++; else q++; } if ((s.size() & 1)) { cout << ":(" << endl; return 0; } if (l > (s.size() / 2) or r > (s.size() / 2)) { cout << ":(" << endl; return 0; } int x = (s.size() / 2) - l, y = (s.size() / 2) - r; int key = 1; for (int i = 0; i < s.size(); i++) { if (a <= b and i != 0) { cout << ":(" << endl; return 0; } if (s[i] == '(') a++; else if (s[i] == ')') b++; else { if (x != 0) { s[i] = '('; x--; a++; } else { s[i] = ')'; y--; b++; } } } if (a != b) { cout << ":(" << endl; } else cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; char s[n]; for (int i = 0; i < n; i++) { cin >> s[i]; } if (n % 2 != 0 || s[0] == ')' || s[n - 1] == '(') { cout << ":("; return 0; } s[0] = '('; s[n - 1] = ')'; map<char, int> m, need; m['?'] = 0; m[')'] = 0; need[')'] = 0; m['('] = 0; need['('] = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') { m['(']++; need[')']++; need['(']--; } if (s[i] == ')') { m[')']++; need['(']++; need[')']--; } if (s[i] == '?') { m['?']++; } } if (need['('] < 0) need['('] = 0; if (need[')'] < 0) need[')'] = 0; if ((m['?'] - m[')'] - m['(']) % 2 != 0) { cout << ":("; return 0; } int col_1 = need['('] + (m['?'] - need[')'] - need['(']) / 2; int col_2 = need[')'] + (m['?'] - need[')'] - need['(']) / 2; for (int i = 1; i < n - 1 && col_1 != 0; i++) { if (s[i] == '?') { s[i] = '('; col_1--; } } for (int i = n - 2; i >= 1 && col_2 != 0; i--) { if (s[i] == '?') { s[i] = ')'; col_2--; } } int depth = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == '(') { depth++; } if (s[i] == ')') { depth--; } if (depth <= 0) { cout << ":("; return 0; } } depth--; if (depth != 0) { cout << ":("; return 0; } for (int i = 0; i < n; i++) { cout << s[i]; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; char s[n]; for (int i = 0; i < n; i++) { cin >> s[i]; } if (n % 2 != 0 || s[0] == ')' || s[n - 1] == '(') { cout << ":("; return 0; } s[0] = '('; s[n - 1] = ')'; map<char, int> m, need; m['?'] = 0; m[')'] = 0; need[')'] = 0; m['('] = 0; need['('] = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') { m['(']++; need[')']++; need['(']--; } if (s[i] == ')') { m[')']++; need['(']++; need[')']--; } if (s[i] == '?') { m['?']++; } } if (need['('] < 0) need['('] = 0; if (need[')'] < 0) need[')'] = 0; if ((m['?'] - m[')'] - m['(']) % 2 != 0) { cout << ":("; return 0; } int col_1 = need['('] + (m['?'] - need[')'] - need['(']) / 2; int col_2 = need[')'] + (m['?'] - need[')'] - need['(']) / 2; for (int i = 1; i < n - 1 && col_1 != 0; i++) { if (s[i] == '?') { s[i] = '('; col_1--; } } for (int i = n - 2; i >= 1 && col_2 != 0; i--) { if (s[i] == '?') { s[i] = ')'; col_2--; } } int depth = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == '(') { depth++; } if (s[i] == ')') { depth--; } if (depth <= 0) { cout << ":("; return 0; } } depth--; if (depth != 0) { cout << ":("; return 0; } for (int i = 0; i < n; i++) { cout << s[i]; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long int N = 2e5 + 5; void solve() { long long int n; cin >> n; string s; cin >> s; if (n % 2 == 1 || s[n - 1] == '(' || s[0] == ')' || ((s[1] == ')') && n != 2)) { cout << ":(" << "\n"; return; } s[0] = '('; s[n - 1] = ')'; if (n != 2) s[1] = '('; long long int bal = 0; long long int c = 0, cc = 0; for (long long int i = 0; i < n; i++) { if (s[i] == '(') { cc++; } } for (long long int i = 0; i < n; i++) { if (s[i] == '?') { if (c < n / 2 - cc) { s[i] = '('; c++; } else { s[i] = ')'; c++; } } } stack<char> st; for (long long int i = 0; i < n; i++) { if (s[i] == ')') { if (st.empty()) { st.push(s[i]); } else if (st.top() == '(') { st.pop(); } else { st.push(s[i]); } } else { st.push(s[i]); } if (st.empty() && i != n - 1) { cout << ":(" << "\n"; return; } } if (st.empty()) { cout << s << "\n"; } else { cout << ":(" << "\n"; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) solve(); return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int N = 2e5 + 5; void solve() { long long int n; cin >> n; string s; cin >> s; if (n % 2 == 1 || s[n - 1] == '(' || s[0] == ')' || ((s[1] == ')') && n != 2)) { cout << ":(" << "\n"; return; } s[0] = '('; s[n - 1] = ')'; if (n != 2) s[1] = '('; long long int bal = 0; long long int c = 0, cc = 0; for (long long int i = 0; i < n; i++) { if (s[i] == '(') { cc++; } } for (long long int i = 0; i < n; i++) { if (s[i] == '?') { if (c < n / 2 - cc) { s[i] = '('; c++; } else { s[i] = ')'; c++; } } } stack<char> st; for (long long int i = 0; i < n; i++) { if (s[i] == ')') { if (st.empty()) { st.push(s[i]); } else if (st.top() == '(') { st.pop(); } else { st.push(s[i]); } } else { st.push(s[i]); } if (st.empty() && i != n - 1) { cout << ":(" << "\n"; return; } } if (st.empty()) { cout << s << "\n"; } else { cout << ":(" << "\n"; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long inf = LLONG_MAX; const long long N = 1e5 + 10; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; string s; cin >> s; if (n & 1) { cout << ":(\n"; return 0; } long long cnt1 = 0, cnt2 = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') { cnt1++; } else if (s[i] == ')') { cnt2++; } } long long x = (n / 2) - cnt1; if (x < 0) { cout << ":(\n"; return 0; } for (long long i = 0; i < n; i++) { if (s[i] == '?' and x > 0) { s[i] = '('; x--; } else if (s[i] == '?') { s[i] = ')'; } } long long cnt = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') { cnt++; } if (s[i] == ')') { cnt--; } if (cnt <= 0 and (i != n - 1)) { cout << ":(\n"; return 0; } } if (cnt == 0) { cout << s << "\n"; } else { cout << ":(\n"; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long inf = LLONG_MAX; const long long N = 1e5 + 10; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; string s; cin >> s; if (n & 1) { cout << ":(\n"; return 0; } long long cnt1 = 0, cnt2 = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') { cnt1++; } else if (s[i] == ')') { cnt2++; } } long long x = (n / 2) - cnt1; if (x < 0) { cout << ":(\n"; return 0; } for (long long i = 0; i < n; i++) { if (s[i] == '?' and x > 0) { s[i] = '('; x--; } else if (s[i] == '?') { s[i] = ')'; } } long long cnt = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') { cnt++; } if (s[i] == ')') { cnt--; } if (cnt <= 0 and (i != n - 1)) { cout << ":(\n"; return 0; } } if (cnt == 0) { cout << s << "\n"; } else { cout << ":(\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long int INF = 1e15; const long long int MOD = 1e9 + 7; template <typename T> using vector2 = vector<vector<T>>; template <typename T> vector2<T> initVec2(size_t n0, size_t n1, T e = T()) { return vector2<T>(n0, vector<T>(n1, e)); } template <typename T> using vector3 = vector<vector<vector<T>>>; template <typename T> vector3<T> initVec3(size_t n0, size_t n1, size_t n2, T e = T()) { return vector3<T>(n0, vector2<T>(n1, vector<T>(n2, e))); } signed main() { ios::sync_with_stdio(false); cin.tie(0); long long int n; string s; cin >> n >> s; if (n % 2) { cout << ":(" << endl; return 0; } long long int l = 0; long long int r = 0; for (long long int i = 0; i < (n); ++i) { if (s[i] == '(') { l++; } if (s[i] == ')') { r++; } } if (l > n / 2 || r > n / 2) { cout << ":(" << endl; return 0; } l = n / 2 - l; for (long long int i = 0; i < (n); ++i) { if (l > 0 && s[i] == '?') { s[i] = '('; l--; continue; } if (s[i] == '?') { s[i] = ')'; } } long long int open = 0; for (long long int i = 0; i < (n); ++i) { if (s[i] == '(') { open++; } else { open--; } if (i != n - 1 && open == 0) { cout << ":(" << endl; return 0; } if (open < 0) { cout << ":(" << endl; return 0; } } cout << s << endl; return 0; }
### Prompt Generate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int INF = 1e15; const long long int MOD = 1e9 + 7; template <typename T> using vector2 = vector<vector<T>>; template <typename T> vector2<T> initVec2(size_t n0, size_t n1, T e = T()) { return vector2<T>(n0, vector<T>(n1, e)); } template <typename T> using vector3 = vector<vector<vector<T>>>; template <typename T> vector3<T> initVec3(size_t n0, size_t n1, size_t n2, T e = T()) { return vector3<T>(n0, vector2<T>(n1, vector<T>(n2, e))); } signed main() { ios::sync_with_stdio(false); cin.tie(0); long long int n; string s; cin >> n >> s; if (n % 2) { cout << ":(" << endl; return 0; } long long int l = 0; long long int r = 0; for (long long int i = 0; i < (n); ++i) { if (s[i] == '(') { l++; } if (s[i] == ')') { r++; } } if (l > n / 2 || r > n / 2) { cout << ":(" << endl; return 0; } l = n / 2 - l; for (long long int i = 0; i < (n); ++i) { if (l > 0 && s[i] == '?') { s[i] = '('; l--; continue; } if (s[i] == '?') { s[i] = ')'; } } long long int open = 0; for (long long int i = 0; i < (n); ++i) { if (s[i] == '(') { open++; } else { open--; } if (i != n - 1 && open == 0) { cout << ":(" << endl; return 0; } if (open < 0) { cout << ":(" << endl; return 0; } } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; stack<char> st; int jud = 0; int main() { int len = 0; string s; cin >> len; if (len == 0) { return 0; } cin >> s; if (len % 2) { cout << ":("; } else { if (s[0] == ')' || s[len - 1] == '(') { jud = 0; cout << ":("; return 0; } int cnt1 = 0, cnt2 = 0; int jud = 1; for (int i = 1; i < len - 1; i++) { if (s[i] == '(') cnt1++; else if (s[i] == ')') cnt2++; } for (int i = 1; i < len - 1; i++) { if (s[i] == '?') { if (cnt1 < len / 2 - 1) { s[i] = '('; st.push(s[i]); cnt1++; } else if (cnt2 < len / 2 - 1) { s[i] = ')'; if (st.size()) st.pop(); else { cout << ":("; jud = 0; break; } cnt2++; } } else if (s[i] == '(') st.push(s[i]); else if (s[i] == ')') { if (st.size()) st.pop(); else { cout << ":("; jud = 0; break; } } } if (jud) { if (!st.size()) { s[0] = '('; s[len - 1] = ')'; cout << s; } else { cout << ":("; } } } getchar(); getchar(); return 0; }
### Prompt In Cpp, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; stack<char> st; int jud = 0; int main() { int len = 0; string s; cin >> len; if (len == 0) { return 0; } cin >> s; if (len % 2) { cout << ":("; } else { if (s[0] == ')' || s[len - 1] == '(') { jud = 0; cout << ":("; return 0; } int cnt1 = 0, cnt2 = 0; int jud = 1; for (int i = 1; i < len - 1; i++) { if (s[i] == '(') cnt1++; else if (s[i] == ')') cnt2++; } for (int i = 1; i < len - 1; i++) { if (s[i] == '?') { if (cnt1 < len / 2 - 1) { s[i] = '('; st.push(s[i]); cnt1++; } else if (cnt2 < len / 2 - 1) { s[i] = ')'; if (st.size()) st.pop(); else { cout << ":("; jud = 0; break; } cnt2++; } } else if (s[i] == '(') st.push(s[i]); else if (s[i] == ')') { if (st.size()) st.pop(); else { cout << ":("; jud = 0; break; } } } if (jud) { if (!st.size()) { s[0] = '('; s[len - 1] = ')'; cout << s; } else { cout << ":("; } } } getchar(); getchar(); return 0; } ```
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; string s; cin >> s; int x = 1, y = 0, a = 1, b = 0; if (n % 2 == 1) { cout << ":("; return 0; } if (s[0] != '?' && s[0] == ')') { cout << ":("; return 0; } if (s[n - 1] != '?' && s[n - 1] == '(') { cout << ":("; return 0; } if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; if (n == 2) cout << s; if (n >= 3) { if (s[1] == ')' or s[n - 2] == '(') { cout << ":("; return 0; } if (s[1] == '?') s[1] = '('; if (s[n - 2] == '?') s[n - 2] = ')'; for (int i = 2; i < n - 1; i++) { if (s[i] == '(') a++; if (s[i] == ')') b++; } int no = n - 2; if (a > no / 2 or b > no / 2) { cout << ":("; return 0; } for (int i = 2; i < n - 1; i++) { if (y > x) { cout << ":("; return 0; } if (s[i] == '?') { if (a < no / 2) { s[i] = '('; x++; a++; } else { s[i] = ')'; b++; y++; } } else { if (s[i] == '(') x++; if (s[i] == ')') y++; } } if (x == y) { cout << s; } else cout << ":("; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; string s; cin >> s; int x = 1, y = 0, a = 1, b = 0; if (n % 2 == 1) { cout << ":("; return 0; } if (s[0] != '?' && s[0] == ')') { cout << ":("; return 0; } if (s[n - 1] != '?' && s[n - 1] == '(') { cout << ":("; return 0; } if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; if (n == 2) cout << s; if (n >= 3) { if (s[1] == ')' or s[n - 2] == '(') { cout << ":("; return 0; } if (s[1] == '?') s[1] = '('; if (s[n - 2] == '?') s[n - 2] = ')'; for (int i = 2; i < n - 1; i++) { if (s[i] == '(') a++; if (s[i] == ')') b++; } int no = n - 2; if (a > no / 2 or b > no / 2) { cout << ":("; return 0; } for (int i = 2; i < n - 1; i++) { if (y > x) { cout << ":("; return 0; } if (s[i] == '?') { if (a < no / 2) { s[i] = '('; x++; a++; } else { s[i] = ')'; b++; y++; } } else { if (s[i] == '(') x++; if (s[i] == ')') y++; } } if (x == y) { cout << s; } else cout << ":("; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n % 2) { cout << ":(" << '\n'; return 0; } int a = n / 2; for (int i = 0; i < n; ++i) { if (s[i] == '(') --a; } if (a < 0) { cout << ":(" << '\n'; return 0; } for (int i = 0; i < n; ++i) { if (s[i] == '?') { if (a > 0) { --a; s[i] = '('; } else { s[i] = ')'; } } } int now = 0; for (int i = 0; i < n; ++i) { if (now <= 0 && i != 0) { cout << ":(" << '\n'; return 0; } if (s[i] == '(') ++now; else --now; } if (now != 0) { cout << ":(" << '\n'; return 0; } cout << s << '\n'; }
### Prompt Your task is to create a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n % 2) { cout << ":(" << '\n'; return 0; } int a = n / 2; for (int i = 0; i < n; ++i) { if (s[i] == '(') --a; } if (a < 0) { cout << ":(" << '\n'; return 0; } for (int i = 0; i < n; ++i) { if (s[i] == '?') { if (a > 0) { --a; s[i] = '('; } else { s[i] = ')'; } } } int now = 0; for (int i = 0; i < n; ++i) { if (now <= 0 && i != 0) { cout << ":(" << '\n'; return 0; } if (s[i] == '(') ++now; else --now; } if (now != 0) { cout << ":(" << '\n'; return 0; } cout << s << '\n'; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int o = 0, z = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') o++; else if (s[i] == ')') z++; } int pola = n / 2; if (n % 2 == 1 || z > pola || o > pola) { cout << ":(" << endl; return 0; } for (int i = 0; i < n && o < n / 2; i++) { if (s[i] == '?') { s[i] = '('; o++; } } int balans = 0; for (int i = 0; i < n; i++) { if (s[i] == '?') s[i] = ')'; if (s[i] == '(') balans++; else balans--; if (balans <= 0 && i < n - 1) { cout << ":(" << endl; return 0; } } cout << s << endl; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int o = 0, z = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') o++; else if (s[i] == ')') z++; } int pola = n / 2; if (n % 2 == 1 || z > pola || o > pola) { cout << ":(" << endl; return 0; } for (int i = 0; i < n && o < n / 2; i++) { if (s[i] == '?') { s[i] = '('; o++; } } int balans = 0; for (int i = 0; i < n; i++) { if (s[i] == '?') s[i] = ')'; if (s[i] == '(') balans++; else balans--; if (balans <= 0 && i < n - 1) { cout << ":(" << endl; return 0; } } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> long long mod(long long x) { if (x < 0) return -x; return x; } long long expo(long long x, long long y) { long long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = x * x; } return res; } using namespace std; const int MAXN = 500005; vector<long long> v; long long a[1000000], b[1000000]; int main() { ios_base::sync_with_stdio(false); long long n, t, m, k, x, y; cin >> n; string s; cin >> s; x = 0; y = 0; t = 0; if (n % 2 != 0) { cout << ":("; return 0; } for (long long i = 0; i < n; i++) { if (s[i] == ')') { y++; } if (s[i] == '(') { x++; } } k = n / 2 - x; for (long long i = 0; i < n; i++) { if (s[i] == '?') { s[i] = '('; k--; } if (k <= 0) break; } k = n / 2 - y; for (long long i = 0; i < n; i++) { if (s[i] == '?') { s[i] = ')'; k--; } if (k <= 0) break; } t = 0; for (long long i = 0; i < n; i++) { if (s[i] == ')') { t--; } if (s[i] == '(') { t++; } if (t <= 0 && i != n - 1) { cout << ":("; return 0; } if (t < 0) { cout << ":("; return 0; } } if (t != 0) { cout << ":("; return 0; } cout << s; return 0; }
### Prompt Please formulate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> long long mod(long long x) { if (x < 0) return -x; return x; } long long expo(long long x, long long y) { long long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = x * x; } return res; } using namespace std; const int MAXN = 500005; vector<long long> v; long long a[1000000], b[1000000]; int main() { ios_base::sync_with_stdio(false); long long n, t, m, k, x, y; cin >> n; string s; cin >> s; x = 0; y = 0; t = 0; if (n % 2 != 0) { cout << ":("; return 0; } for (long long i = 0; i < n; i++) { if (s[i] == ')') { y++; } if (s[i] == '(') { x++; } } k = n / 2 - x; for (long long i = 0; i < n; i++) { if (s[i] == '?') { s[i] = '('; k--; } if (k <= 0) break; } k = n / 2 - y; for (long long i = 0; i < n; i++) { if (s[i] == '?') { s[i] = ')'; k--; } if (k <= 0) break; } t = 0; for (long long i = 0; i < n; i++) { if (s[i] == ')') { t--; } if (s[i] == '(') { t++; } if (t <= 0 && i != n - 1) { cout << ":("; return 0; } if (t < 0) { cout << ":("; return 0; } } if (t != 0) { cout << ":("; return 0; } cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, n1, n2; string s; void fk() { cout << ":("; exit(0); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> s; if (n & 1) fk(); for (char c : s) { if (c == '(') ++n1; else if (c == ')') ++n2; } if (n1 > n / 2 || n2 > n / 2) fk(); n1 = n / 2 - n1; for (char &c : s) { if (c == '?') { if (n1) { --n1; c = '('; } else { c = ')'; } } } int b = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == '(') ++b; else --b; if (b <= 0) fk(); } cout << s; }
### Prompt Your challenge is to write a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, n1, n2; string s; void fk() { cout << ":("; exit(0); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> s; if (n & 1) fk(); for (char c : s) { if (c == '(') ++n1; else if (c == ')') ++n2; } if (n1 > n / 2 || n2 > n / 2) fk(); n1 = n / 2 - n1; for (char &c : s) { if (c == '?') { if (n1) { --n1; c = '('; } else { c = ')'; } } } int b = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == '(') ++b; else --b; if (b <= 0) fk(); } cout << s; } ```
#include <bits/stdc++.h> using namespace std; int a[1000005]; bool balParenthesis(string exp) { stack<char> stack; for (int i = 0; i < exp.length(); i++) { if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[') { stack.push(exp[i]); } if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']') { if (stack.empty()) { return false; } char top = stack.top(); stack.pop(); if ((top == '(' && exp[i] != ')') || (top == '{' && exp[i] != '}') || (top == '[' && exp[i] != ']')) { return false; } } } return stack.empty(); } int main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); int i, j, k, l, m, n, t; cin >> n; string str; string no = ":("; cin >> str; if (str[0] == ')' || str[n - 1] == '(') { cout << no << endl; return 0; } str[0] = '('; str[n - 1] = ')'; int nop = 0, nc = 0; for (i = 1; i < n - 1; i++) { if (str[i] == '(') nop++; else if (str[i] == ')') nc++; } int tot = n - 2; if (tot & 1) { cout << no << endl; return 0; } int ro = tot / 2 - nop; int rc = tot / 2 - nc; if (ro < 0 || rc < 0) { cout << no << endl; return 0; } int op = 0; for (i = 1; i < n - 1; i++) { if (str[i] == '?') { if (ro) { ro--; str[i] = '('; } else { rc--; str[i] = ')'; } } } string str2 = str.substr(1, n - 2); if (!balParenthesis(str) || !balParenthesis(str2)) { cout << no << endl; return 0; } cout << str << endl; }
### Prompt Construct a cpp code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[1000005]; bool balParenthesis(string exp) { stack<char> stack; for (int i = 0; i < exp.length(); i++) { if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[') { stack.push(exp[i]); } if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']') { if (stack.empty()) { return false; } char top = stack.top(); stack.pop(); if ((top == '(' && exp[i] != ')') || (top == '{' && exp[i] != '}') || (top == '[' && exp[i] != ']')) { return false; } } } return stack.empty(); } int main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); int i, j, k, l, m, n, t; cin >> n; string str; string no = ":("; cin >> str; if (str[0] == ')' || str[n - 1] == '(') { cout << no << endl; return 0; } str[0] = '('; str[n - 1] = ')'; int nop = 0, nc = 0; for (i = 1; i < n - 1; i++) { if (str[i] == '(') nop++; else if (str[i] == ')') nc++; } int tot = n - 2; if (tot & 1) { cout << no << endl; return 0; } int ro = tot / 2 - nop; int rc = tot / 2 - nc; if (ro < 0 || rc < 0) { cout << no << endl; return 0; } int op = 0; for (i = 1; i < n - 1; i++) { if (str[i] == '?') { if (ro) { ro--; str[i] = '('; } else { rc--; str[i] = ')'; } } } string str2 = str.substr(1, n - 2); if (!balParenthesis(str) || !balParenthesis(str2)) { cout << no << endl; return 0; } cout << str << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; int n; cin >> n; cin >> s; if (n % 2 == 1) { cout << ":("; return 0; } if (s[0] == ')') { cout << ":("; return 0; } if (s[0] == '?') { s[0] = '('; } if (s[n - 1] == '(') { cout << ":("; return 0; } if (s[n - 1] == '?') { s[n - 1] = ')'; } int br[n - 1]; br[0] = 0; int count = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') br[i] = br[i - 1] + 1; else if (s[i] == ')') { br[i] = br[i - 1] - 1; } else { count++; br[i] = br[i - 1]; } } int diff = br[n - 2]; int right = (count + diff) / 2; int left = count - right; int p1 = 0; int i = 0; bool flag = 1; while (i < n - 1) { if (s[i] == '?' && flag == 1) { if (p1 == left) { flag = 0; } else { p1++; s[i] = '('; } } if (s[i] == '?' && flag == 0) { s[i] = ')'; } i++; } int temp[n - 1]; temp[0] = 0; for (int i = 1; i < n - 1; i++) { if (temp[i - 1] < 0) { cout << ":("; return 0; } if (s[i] == '(') temp[i] = temp[i - 1] + 1; else if (s[i] == ')') { temp[i] = temp[i - 1] - 1; } } if (temp[n - 2] != 0) { cout << ":("; return 0; } cout << s; }
### Prompt Please provide a cpp coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; int n; cin >> n; cin >> s; if (n % 2 == 1) { cout << ":("; return 0; } if (s[0] == ')') { cout << ":("; return 0; } if (s[0] == '?') { s[0] = '('; } if (s[n - 1] == '(') { cout << ":("; return 0; } if (s[n - 1] == '?') { s[n - 1] = ')'; } int br[n - 1]; br[0] = 0; int count = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') br[i] = br[i - 1] + 1; else if (s[i] == ')') { br[i] = br[i - 1] - 1; } else { count++; br[i] = br[i - 1]; } } int diff = br[n - 2]; int right = (count + diff) / 2; int left = count - right; int p1 = 0; int i = 0; bool flag = 1; while (i < n - 1) { if (s[i] == '?' && flag == 1) { if (p1 == left) { flag = 0; } else { p1++; s[i] = '('; } } if (s[i] == '?' && flag == 0) { s[i] = ')'; } i++; } int temp[n - 1]; temp[0] = 0; for (int i = 1; i < n - 1; i++) { if (temp[i - 1] < 0) { cout << ":("; return 0; } if (s[i] == '(') temp[i] = temp[i - 1] + 1; else if (s[i] == ')') { temp[i] = temp[i - 1] - 1; } } if (temp[n - 2] != 0) { cout << ":("; return 0; } cout << s; } ```
#include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { int n; cin >> n; string str; cin >> str; int lcnt = 0, rcnt = 0, qcnt = 0; for (int i = str.length() - 1; i >= 0; i--) { if (str[i] == '(') lcnt++; else if (str[i] == ')') rcnt++; else qcnt++; } int val = lcnt - rcnt; if ((qcnt + lcnt - rcnt) % 2 == 1) { cout << ":("; return 0; } int rr = (qcnt + lcnt - rcnt) / 2; int ll = qcnt - rr; lcnt = 0, rcnt = 0; for (int i = 0; i < str.length(); i++) { if (str[i] == '(') lcnt++; else if (str[i] == ')') rcnt++; if (ll > 0 && str[i] == '?') { ll--; lcnt++; str[i] = '('; } if (rr > 0 && str[i] == '?') { rr--; rcnt++; str[i] = ')'; } if (lcnt <= rcnt && i != str.length() - 1) { cout << ":("; return 0; } } if (lcnt != rcnt) { cout << ":("; return 0; } cout << str << endl; } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { int n; cin >> n; string str; cin >> str; int lcnt = 0, rcnt = 0, qcnt = 0; for (int i = str.length() - 1; i >= 0; i--) { if (str[i] == '(') lcnt++; else if (str[i] == ')') rcnt++; else qcnt++; } int val = lcnt - rcnt; if ((qcnt + lcnt - rcnt) % 2 == 1) { cout << ":("; return 0; } int rr = (qcnt + lcnt - rcnt) / 2; int ll = qcnt - rr; lcnt = 0, rcnt = 0; for (int i = 0; i < str.length(); i++) { if (str[i] == '(') lcnt++; else if (str[i] == ')') rcnt++; if (ll > 0 && str[i] == '?') { ll--; lcnt++; str[i] = '('; } if (rr > 0 && str[i] == '?') { rr--; rcnt++; str[i] = ')'; } if (lcnt <= rcnt && i != str.length() - 1) { cout << ":("; return 0; } } if (lcnt != rcnt) { cout << ":("; return 0; } cout << str << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 10; const long long mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-7; inline long long read() { long long X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } char str[maxn]; int main() { int n = read(); scanf("%s", str); if (n % 2 != 0) { cout << ":(" << endl; return 0; } int cnt1 = 0, cnt2 = 0; for (int i = 0; i < n; ++i) { if (str[i] == '(') cnt1++; else if (str[i] == ')') cnt2++; } for (int i = 0; i < n; ++i) { if (str[i] == '?') { if (cnt1 < n / 2) str[i] = '(', cnt1++; else str[i] = ')', cnt2++; } } stack<int> st; for (int i = 0; i < n; ++i) { if (str[i] == '(') { st.push(1); } else { if (st.empty()) { cout << ":(" << endl; return 0; } else { st.pop(); } if (st.empty() && i != n - 1) { cout << ":(" << endl; return 0; } } } if (st.size()) { cout << ":(" << endl; } else cout << str << endl; return 0; }
### Prompt Generate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 10; const long long mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-7; inline long long read() { long long X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } char str[maxn]; int main() { int n = read(); scanf("%s", str); if (n % 2 != 0) { cout << ":(" << endl; return 0; } int cnt1 = 0, cnt2 = 0; for (int i = 0; i < n; ++i) { if (str[i] == '(') cnt1++; else if (str[i] == ')') cnt2++; } for (int i = 0; i < n; ++i) { if (str[i] == '?') { if (cnt1 < n / 2) str[i] = '(', cnt1++; else str[i] = ')', cnt2++; } } stack<int> st; for (int i = 0; i < n; ++i) { if (str[i] == '(') { st.push(1); } else { if (st.empty()) { cout << ":(" << endl; return 0; } else { st.pop(); } if (st.empty() && i != n - 1) { cout << ":(" << endl; return 0; } } } if (st.size()) { cout << ":(" << endl; } else cout << str << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const double EPS = 1e-8; const double PI = 2 * acos(0); const int MAXN = 3e5 + 6; int n; string s; int unclose[MAXN], unopen[MAXN]; bool input() { cin >> n >> s; return !cin.fail(); } bool first_check() { if (s[0] == ')' || s[n - 1] == '(') return false; if (n % 2 == 1) return false; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; return true; } bool second_check() { unclose[0] = 0; unopen[n - 1] = 0; int lastques = n; for (int i = n - 2; i > 0; --i) { if (s[i] == '(') { if (unopen[i + 1] > 0) { unopen[i] = unopen[i + 1] - 1; } else { while (lastques > i && s[lastques] != '?') lastques--; if (lastques > i) { s[lastques] = ')'; unopen[i] = 0; } else { return false; } } } else if (s[i] == ')') unopen[i] = unopen[i + 1] + 1; else if (s[i] == '?') unopen[i] = unopen[i + 1]; } int firstques = 0; for (int i = 1; i < n - 1; ++i) { if (s[i] == ')') { if (unclose[i - 1] > 0) { unclose[i] = unclose[i - 1] - 1; } else { while (firstques < i && s[firstques] != '?') firstques++; if (firstques < i) { s[firstques] = '('; unclose[i] = 0; } else { return false; } } } else if (s[i] == '(') unclose[i] = unclose[i - 1] + 1; else if (s[i] == '?') unclose[i] = unclose[i - 1]; } return true; } void process() { if (!first_check()) { s = ":("; return; } if (!second_check()) { s = ":("; return; } unclose[0] = 0; unopen[n - 1] = 0; for (int i = n - 2; i > 0; --i) { if (s[i] == '(') unopen[i] = unopen[i + 1] - 1; if (s[i] == ')') unopen[i] = unopen[i + 1] + 1; if (s[i] == '?') unopen[i] = unopen[i + 1]; } for (int i = 1; i < n - 1; ++i) { if (s[i] == '(') unclose[i] = unclose[i - 1] + 1; if (s[i] == ')') unclose[i] = unclose[i - 1] - 1; if (s[i] == '?') { unclose[i] = unclose[i - 1]; if (unclose[i] <= unopen[i]) { s[i] = '('; unclose[i]++; } else { s[i] = ')'; unclose[i]--; } } } int nopen = 0; for (int i = 1; i < n - 1; ++i) { if (s[i] == '(') { nopen++; } else { nopen--; } if (nopen < 0) { s = ":("; return; } } if (nopen != 0) { s = ":("; return; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); while (input()) { process(); cout << s << endl; } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const double EPS = 1e-8; const double PI = 2 * acos(0); const int MAXN = 3e5 + 6; int n; string s; int unclose[MAXN], unopen[MAXN]; bool input() { cin >> n >> s; return !cin.fail(); } bool first_check() { if (s[0] == ')' || s[n - 1] == '(') return false; if (n % 2 == 1) return false; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; return true; } bool second_check() { unclose[0] = 0; unopen[n - 1] = 0; int lastques = n; for (int i = n - 2; i > 0; --i) { if (s[i] == '(') { if (unopen[i + 1] > 0) { unopen[i] = unopen[i + 1] - 1; } else { while (lastques > i && s[lastques] != '?') lastques--; if (lastques > i) { s[lastques] = ')'; unopen[i] = 0; } else { return false; } } } else if (s[i] == ')') unopen[i] = unopen[i + 1] + 1; else if (s[i] == '?') unopen[i] = unopen[i + 1]; } int firstques = 0; for (int i = 1; i < n - 1; ++i) { if (s[i] == ')') { if (unclose[i - 1] > 0) { unclose[i] = unclose[i - 1] - 1; } else { while (firstques < i && s[firstques] != '?') firstques++; if (firstques < i) { s[firstques] = '('; unclose[i] = 0; } else { return false; } } } else if (s[i] == '(') unclose[i] = unclose[i - 1] + 1; else if (s[i] == '?') unclose[i] = unclose[i - 1]; } return true; } void process() { if (!first_check()) { s = ":("; return; } if (!second_check()) { s = ":("; return; } unclose[0] = 0; unopen[n - 1] = 0; for (int i = n - 2; i > 0; --i) { if (s[i] == '(') unopen[i] = unopen[i + 1] - 1; if (s[i] == ')') unopen[i] = unopen[i + 1] + 1; if (s[i] == '?') unopen[i] = unopen[i + 1]; } for (int i = 1; i < n - 1; ++i) { if (s[i] == '(') unclose[i] = unclose[i - 1] + 1; if (s[i] == ')') unclose[i] = unclose[i - 1] - 1; if (s[i] == '?') { unclose[i] = unclose[i - 1]; if (unclose[i] <= unopen[i]) { s[i] = '('; unclose[i]++; } else { s[i] = ')'; unclose[i]--; } } } int nopen = 0; for (int i = 1; i < n - 1; ++i) { if (s[i] == '(') { nopen++; } else { nopen--; } if (nopen < 0) { s = ":("; return; } } if (nopen != 0) { s = ":("; return; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); while (input()) { process(); cout << s << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int f[101], l[101], t[101][101], ans[101][101]; int n, m, h; int main() { int n, cnt = 0; string s; cin >> n >> s; if (n & 1) { cout << ":("; return 0; } for (int i = 0; i < n; ++i) if (s[i] == '(') ++cnt; for (int i = 0; i < n; ++i) { if (s[i] == '?') { if (cnt < n / 2) { s[i] = '('; ++cnt; } else { s[i] = ')'; } } } cnt = 0; for (int i = 0; i < n; ++i) { if (s[i] == '(') ++cnt; else --cnt; if (i != n - 1 && cnt <= 0) { cout << ":("; return 0; } } if (cnt != 0) { cout << ":("; return 0; } cout << s; return 0; }
### Prompt Please formulate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int f[101], l[101], t[101][101], ans[101][101]; int n, m, h; int main() { int n, cnt = 0; string s; cin >> n >> s; if (n & 1) { cout << ":("; return 0; } for (int i = 0; i < n; ++i) if (s[i] == '(') ++cnt; for (int i = 0; i < n; ++i) { if (s[i] == '?') { if (cnt < n / 2) { s[i] = '('; ++cnt; } else { s[i] = ')'; } } } cnt = 0; for (int i = 0; i < n; ++i) { if (s[i] == '(') ++cnt; else --cnt; if (i != n - 1 && cnt <= 0) { cout << ":("; return 0; } } if (cnt != 0) { cout << ":("; return 0; } cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; string s; int st[500005]; int R; bool check(string s) { for (int i = 1; i <= n; i++) { if (s[i - 1] == '(') st[++R] = 1; else R--; if (R < 0) return false; if (i != n && R == 0) return false; if (i == n && R != 0) return false; } return true; } int main() { cin >> n; cin >> s; int l = 0, r = 0; for (int i = 1; i <= n; i++) { if (s[i - 1] == '(') l++; } if (n % 2) { puts(":("); return 0; } for (int i = 1; i <= n; i++) { if (s[i - 1] == '?') { if (l < n / 2) { l++; s[i - 1] = '('; } else { r++; s[i - 1] = ')'; } } } if (!check(s)) puts(":("); else cout << s << endl; }
### Prompt Please formulate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; string s; int st[500005]; int R; bool check(string s) { for (int i = 1; i <= n; i++) { if (s[i - 1] == '(') st[++R] = 1; else R--; if (R < 0) return false; if (i != n && R == 0) return false; if (i == n && R != 0) return false; } return true; } int main() { cin >> n; cin >> s; int l = 0, r = 0; for (int i = 1; i <= n; i++) { if (s[i - 1] == '(') l++; } if (n % 2) { puts(":("); return 0; } for (int i = 1; i <= n; i++) { if (s[i - 1] == '?') { if (l < n / 2) { l++; s[i - 1] = '('; } else { r++; s[i - 1] = ')'; } } } if (!check(s)) puts(":("); else cout << s << endl; } ```
#include <bits/stdc++.h> using namespace std; int gi() { int a; scanf("%d", &a); return a; } long long gli() { long long a; scanf("%I64d", &a); return a; } char a[300004]; void d() { printf(":(\n"); exit(0); } int main() { int n = gi(); if (n % 2) d(); scanf("%s", a); int o = 0, c = 0; for (int i = 0; i < n; i++) if (a[i] == '(') o++; else if (a[i] == ')') c++; if (o > n / 2 || c > n / 2) d(); o = n / 2 - o; int cn = 0; for (int i = 0; i < n; i++) { if (a[i] == '?') { if (o) { o--; a[i] = '('; } else a[i] = ')'; } if (a[i] == '(') cn++; else cn--; if (i < n - 1 && cn <= 0) d(); } printf("%s\n", a); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int gi() { int a; scanf("%d", &a); return a; } long long gli() { long long a; scanf("%I64d", &a); return a; } char a[300004]; void d() { printf(":(\n"); exit(0); } int main() { int n = gi(); if (n % 2) d(); scanf("%s", a); int o = 0, c = 0; for (int i = 0; i < n; i++) if (a[i] == '(') o++; else if (a[i] == ')') c++; if (o > n / 2 || c > n / 2) d(); o = n / 2 - o; int cn = 0; for (int i = 0; i < n; i++) { if (a[i] == '?') { if (o) { o--; a[i] = '('; } else a[i] = ')'; } if (a[i] == '(') cn++; else cn--; if (i < n - 1 && cn <= 0) d(); } printf("%s\n", a); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 100050; int main() { int n; scanf("%d", &n); string s; cin >> s; if (n % 2 == 1) { puts(":("); return 0; } if (s[0] == ')') { puts(":("); return 0; } else if (s[0] == '?') { s[0] = '('; } int cntl = 0; queue<int> q; for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { q.push(i); } else if (s[i] == '(') { cntl++; } else if (s[i] == ')') { if (cntl > 0) { cntl--; } else if (cntl == 0 && !q.empty()) { int now = q.front(); q.pop(); s[now] = '('; } else { puts(":("); return 0; } } } if (s[n - 1] == '?') s[n - 1] = ')'; int len = q.size(); int cnt = 0; while (!q.empty()) { int now = q.front(); q.pop(); cnt++; if (cnt <= (len - cntl) / 2) { s[now] = '('; } else s[now] = ')'; } cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') { cnt++; } else { cnt--; } if (cnt <= 0 && i < n - 1) { puts(":("); return 0; } } if (cnt == 0) { cout << s << endl; } else { puts(":("); } return 0; }
### Prompt In Cpp, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 100050; int main() { int n; scanf("%d", &n); string s; cin >> s; if (n % 2 == 1) { puts(":("); return 0; } if (s[0] == ')') { puts(":("); return 0; } else if (s[0] == '?') { s[0] = '('; } int cntl = 0; queue<int> q; for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { q.push(i); } else if (s[i] == '(') { cntl++; } else if (s[i] == ')') { if (cntl > 0) { cntl--; } else if (cntl == 0 && !q.empty()) { int now = q.front(); q.pop(); s[now] = '('; } else { puts(":("); return 0; } } } if (s[n - 1] == '?') s[n - 1] = ')'; int len = q.size(); int cnt = 0; while (!q.empty()) { int now = q.front(); q.pop(); cnt++; if (cnt <= (len - cntl) / 2) { s[now] = '('; } else s[now] = ')'; } cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') { cnt++; } else { cnt--; } if (cnt <= 0 && i < n - 1) { puts(":("); return 0; } } if (cnt == 0) { cout << s << endl; } else { puts(":("); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; if (n == 0) { cout << endl; return 0; } if (n % 2 != 0) { cout << ":(" << endl; return 0; } string s; long long count1 = 0; cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') count1++; else if (s[i] == ')') count1--; } for (int i = s.size() - 1; i >= 0 && count1 > 0; i--) { if (s[i] == '?') { s[i] = ')'; count1--; } } for (int i = 0; i < s.size() && count1 < 0; i++) { if (s[i] == '?') { s[i] = '('; count1++; } } int i = 0, j = s.size() - 1; while (i < j) { while (s[i] != '?' && i < s.size()) i++; if (s[i] == '?') s[i] = '('; while (s[j] != '?' && j >= 0) j--; if (s[j] == '?') s[j] = ')'; } int count2 = 0; for (i = 0; i < s.size() && count2 >= 0; i++) { if (s[i] == '(') count2++; else if (s[i] == ')') count2--; if (count2 <= 0) { i++; break; } } if (count2 != 0 || i != n) cout << ":(" << endl; else { cout << s << endl; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; if (n == 0) { cout << endl; return 0; } if (n % 2 != 0) { cout << ":(" << endl; return 0; } string s; long long count1 = 0; cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') count1++; else if (s[i] == ')') count1--; } for (int i = s.size() - 1; i >= 0 && count1 > 0; i--) { if (s[i] == '?') { s[i] = ')'; count1--; } } for (int i = 0; i < s.size() && count1 < 0; i++) { if (s[i] == '?') { s[i] = '('; count1++; } } int i = 0, j = s.size() - 1; while (i < j) { while (s[i] != '?' && i < s.size()) i++; if (s[i] == '?') s[i] = '('; while (s[j] != '?' && j >= 0) j--; if (s[j] == '?') s[j] = ')'; } int count2 = 0; for (i = 0; i < s.size() && count2 >= 0; i++) { if (s[i] == '(') count2++; else if (s[i] == ')') count2--; if (count2 <= 0) { i++; break; } } if (count2 != 0 || i != n) cout << ":(" << endl; else { cout << s << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string s; int n; cin >> n >> s; if (n % 2) { cout << ":("; return 0; } int cls = 0; int opn = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') opn++; if (s[i] == ')') cls++; } int needOpen = n / 2 - opn; int needCls = n / 2 - cls; if (needOpen < 0 || needCls < 0) { cout << ":("; return 0; } for (int i = 0; i < n; i++) { if (needOpen > 0 && s[i] == '?') { needOpen--; s[i] = '('; } } for (int i = n - 1; i >= 0; i--) { if (needCls > 0 && s[i] == '?') { needCls--; s[i] = ')'; } } if (needCls != 0 || needOpen != 0) { cout << ":("; return 0; } queue<int> q; for (int i = 0; i < n; i++) { if (s[i] == '(') { q.push(i); } else if (s[i] == ')') { if (q.size() == 0) { cout << ":("; return 0; } else { q.pop(); } } else { cout << ":("; return 0; } if (q.size() == 0 && i != n - 1) { cout << ":("; return 0; } } cout << s << endl; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string s; int n; cin >> n >> s; if (n % 2) { cout << ":("; return 0; } int cls = 0; int opn = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') opn++; if (s[i] == ')') cls++; } int needOpen = n / 2 - opn; int needCls = n / 2 - cls; if (needOpen < 0 || needCls < 0) { cout << ":("; return 0; } for (int i = 0; i < n; i++) { if (needOpen > 0 && s[i] == '?') { needOpen--; s[i] = '('; } } for (int i = n - 1; i >= 0; i--) { if (needCls > 0 && s[i] == '?') { needCls--; s[i] = ')'; } } if (needCls != 0 || needOpen != 0) { cout << ":("; return 0; } queue<int> q; for (int i = 0; i < n; i++) { if (s[i] == '(') { q.push(i); } else if (s[i] == ')') { if (q.size() == 0) { cout << ":("; return 0; } else { q.pop(); } } else { cout << ":("; return 0; } if (q.size() == 0 && i != n - 1) { cout << ":("; return 0; } } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; void solve() { int n; cin >> n; string s; cin >> s; int cnt0 = 0, cnt1 = 0; if (n % 2 == 1) { cout << ":(\n"; return; } for (int i = 0; i < n; i++) { if (s[i] == '(') cnt0++; else if (s[i] == ')') cnt1++; } int rem0 = n / 2 - cnt0; int rem1 = n / 2 - cnt1; cnt1 = 0; cnt0 = 0; if (rem0 < 0 || rem1 < 0) { cout << ":(\n"; return; } for (int i = 0; i < n; i++) { if (s[i] == '?') { if (rem0) { s[i] = '('; cnt0++; rem0--; } else { s[i] = ')'; cnt1++; rem1--; } } else if (s[i] == '(') { cnt0++; } else cnt1++; if (cnt1 >= cnt0 && i < n - 1) { cout << ":(\n"; return; } } if (cnt0 == cnt1) cout << s << "\n"; else cout << ":(\n"; } int main() { ios_base::sync_with_stdio(false); int test; test = 1; while (test--) { solve(); } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; void solve() { int n; cin >> n; string s; cin >> s; int cnt0 = 0, cnt1 = 0; if (n % 2 == 1) { cout << ":(\n"; return; } for (int i = 0; i < n; i++) { if (s[i] == '(') cnt0++; else if (s[i] == ')') cnt1++; } int rem0 = n / 2 - cnt0; int rem1 = n / 2 - cnt1; cnt1 = 0; cnt0 = 0; if (rem0 < 0 || rem1 < 0) { cout << ":(\n"; return; } for (int i = 0; i < n; i++) { if (s[i] == '?') { if (rem0) { s[i] = '('; cnt0++; rem0--; } else { s[i] = ')'; cnt1++; rem1--; } } else if (s[i] == '(') { cnt0++; } else cnt1++; if (cnt1 >= cnt0 && i < n - 1) { cout << ":(\n"; return; } } if (cnt0 == cnt1) cout << s << "\n"; else cout << ":(\n"; } int main() { ios_base::sync_with_stdio(false); int test; test = 1; while (test--) { solve(); } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") const long double EPSILON = 0.000000001; const long long int mod = 1000000007; const long long int AM = 2e5 + 5; const long double pi = 3.14159265358979323846; long long int max(long long int a, long long int b) { if (a > b) return a; else return b; } using namespace std; long long int fact(long long int n) { long long int ans = 1; for (long long int i = 1; i <= n; i++) ans *= i; return ans; } long long int powm(long long int a, long long int b, long long int n) { long long int x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y) % n; } y = (y * y) % n; b /= 2; } return x % n; } long long int pow(long long int x, long long int y) { long long int res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; } long long int min(long long int a, long long int b) { if (a > b) return b; else return a; } bool cmp(long double x, long double y) { if (abs(x - y) <= EPSILON) return 1; return 0; } int main() { long long int n; cin >> n; string s; long long int bl = 0; cin >> s; if (s[0] == ')' || s[n - 1] == '(') { cout << ":(" << "\n"; return 0; } int a = 0; for (int i = 0; i < n; i++) if (s[i] == '(') a++; for (int i = 0; i < n - 1; i++) { if (s[i] == ')') bl--; else if (s[i] == '(') bl++; else { if (a < n / 2) { a++; s[i] = '('; bl++; } else { s[i] = ')'; bl--; } } if (bl <= 0) { cout << ":(" << "\n"; return 0; } } s[n - 1] = ')'; bl--; if (!bl) cout << s << "\n"; else cout << ":(" << "\n"; return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") const long double EPSILON = 0.000000001; const long long int mod = 1000000007; const long long int AM = 2e5 + 5; const long double pi = 3.14159265358979323846; long long int max(long long int a, long long int b) { if (a > b) return a; else return b; } using namespace std; long long int fact(long long int n) { long long int ans = 1; for (long long int i = 1; i <= n; i++) ans *= i; return ans; } long long int powm(long long int a, long long int b, long long int n) { long long int x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y) % n; } y = (y * y) % n; b /= 2; } return x % n; } long long int pow(long long int x, long long int y) { long long int res = 1; while (y > 0) { if (y & 1) res = ((res) * (x)); y = y >> 1; x = ((x) * (x)); } return res; } long long int min(long long int a, long long int b) { if (a > b) return b; else return a; } bool cmp(long double x, long double y) { if (abs(x - y) <= EPSILON) return 1; return 0; } int main() { long long int n; cin >> n; string s; long long int bl = 0; cin >> s; if (s[0] == ')' || s[n - 1] == '(') { cout << ":(" << "\n"; return 0; } int a = 0; for (int i = 0; i < n; i++) if (s[i] == '(') a++; for (int i = 0; i < n - 1; i++) { if (s[i] == ')') bl--; else if (s[i] == '(') bl++; else { if (a < n / 2) { a++; s[i] = '('; bl++; } else { s[i] = ')'; bl--; } } if (bl <= 0) { cout << ":(" << "\n"; return 0; } } s[n - 1] = ')'; bl--; if (!bl) cout << s << "\n"; else cout << ":(" << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; int dir4[4][2] = {1, 0, 0, 1, -1, 0, 0, -1}; const int PI = acos(-1); const int INF = 0x3f3f3f3f; const int maxn = 1e2 + 10; const int mod = 998244353; stack<char> st; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; int n; cin >> n >> s; int a, b; a = b = 0; for (char c : s) { a += c == '('; b += c == ')'; } for (int i = 0; i < s.size(); i++) { if (s[i] == '?') { if (a < n / 2) { s[i] = '('; a++; } else { s[i] = ')'; b++; } } } int res = 0; for (char i : s) { if (!st.empty() && (st.top() == '(' && i == ')')) st.pop(); else st.push(i); if (st.empty()) res++; } if (res == 1 && st.empty()) cout << (s) << endl; else cout << (":(") << endl; return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; int dir4[4][2] = {1, 0, 0, 1, -1, 0, 0, -1}; const int PI = acos(-1); const int INF = 0x3f3f3f3f; const int maxn = 1e2 + 10; const int mod = 998244353; stack<char> st; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; int n; cin >> n >> s; int a, b; a = b = 0; for (char c : s) { a += c == '('; b += c == ')'; } for (int i = 0; i < s.size(); i++) { if (s[i] == '?') { if (a < n / 2) { s[i] = '('; a++; } else { s[i] = ')'; b++; } } } int res = 0; for (char i : s) { if (!st.empty() && (st.top() == '(' && i == ')')) st.pop(); else st.push(i); if (st.empty()) res++; } if (res == 1 && st.empty()) cout << (s) << endl; else cout << (":(") << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10; inline int read() { register int x = 0, f = 0; register char c = getchar(); while (c < '0' || c > '9') f |= c == '-', c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar(); return f ? -x : x; } int n; char s[N]; void solve() { int n1 = n / 2, n2 = n / 2; s[1] = '('; for (int i = 1; i <= n; ++i) n1 -= (s[i] == '('), n2 -= (s[i] == ')'); int res = 0; for (int i = 1; i <= n; ++i) { if (s[i] == '(') res++; else if (s[i] == ')') res--; else if (s[i] == '?') { if (n1) s[i] = '(', res++, n1--; else s[i] = ')', res--, n2--; } if (i < n && res == 0) { puts(":("); return; } } if (res != 0) { puts(":("); return; } cout << (s + 1) << '\n'; return; } int main() { n = read(); scanf("%s", s + 1); if ((n & 1) || (s[1] == ')')) puts(":("); else solve(); return 0; }
### Prompt Create a solution in cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10; inline int read() { register int x = 0, f = 0; register char c = getchar(); while (c < '0' || c > '9') f |= c == '-', c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar(); return f ? -x : x; } int n; char s[N]; void solve() { int n1 = n / 2, n2 = n / 2; s[1] = '('; for (int i = 1; i <= n; ++i) n1 -= (s[i] == '('), n2 -= (s[i] == ')'); int res = 0; for (int i = 1; i <= n; ++i) { if (s[i] == '(') res++; else if (s[i] == ')') res--; else if (s[i] == '?') { if (n1) s[i] = '(', res++, n1--; else s[i] = ')', res--, n2--; } if (i < n && res == 0) { puts(":("); return; } } if (res != 0) { puts(":("); return; } cout << (s + 1) << '\n'; return; } int main() { n = read(); scanf("%s", s + 1); if ((n & 1) || (s[1] == ')')) puts(":("); else solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; int n, l = 0; cin >> n; cin >> s; int k = n / 2; if (n % 2 || s[0] == ')' || s[n - 1] == '(') { cout << ":(" << endl; return 0; } for (int i = 0; i < n; ++i) { if (s[i] == '(') { k--; } } for (int i = 0; i < n; ++i) { if (k && s[i] == '?') { s[i] = '('; k--; } else if (!k && s[i] == '?') { s[i] = ')'; } } for (int i = 0; i < n - 1; ++i) { if (s[i] == '(') { l++; } else { l--; } if (l <= 0) { cout << ":(" << endl; return 0; } } l--; if (!l) { cout << s << endl; return 0; } else { cout << ":(" << endl; return 0; } }
### Prompt Please create a solution in Cpp to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; int n, l = 0; cin >> n; cin >> s; int k = n / 2; if (n % 2 || s[0] == ')' || s[n - 1] == '(') { cout << ":(" << endl; return 0; } for (int i = 0; i < n; ++i) { if (s[i] == '(') { k--; } } for (int i = 0; i < n; ++i) { if (k && s[i] == '?') { s[i] = '('; k--; } else if (!k && s[i] == '?') { s[i] = ')'; } } for (int i = 0; i < n - 1; ++i) { if (s[i] == '(') { l++; } else { l--; } if (l <= 0) { cout << ":(" << endl; return 0; } } l--; if (!l) { cout << s << endl; return 0; } else { cout << ":(" << endl; return 0; } } ```
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-ffloat-store") #pragma GCC optimize("-fno-defer-pop") long long int power(long long int a, long long int b, long long int m) { if (b == 0) return 1; if (b == 1) return a % m; long long int t = power(a, b / 2, m) % m; t = (t * t) % m; if (b & 1) t = ((t % m) * (a % m)) % m; return t; } long long int modInverse(long long int a, long long int m) { return power(a, m - 2, m); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int i, j, k, l, n; cin >> n; char ar[n + 1]; cin >> ar; if (n % 2 == 1) { cout << ":("; return 0; } long long int a = 0, b = 0, c = 0; for (i = 0; i < n; i++) { if (ar[i] == '?') c++; else if (ar[i] == '(') a++; else b++; } a = n / 2 - a; b = n / 2 - b; for (i = 0; i < n; i++) { if (ar[i] == '?') { if (a > 0) { ar[i] = '('; a--; } else { ar[i] = ')'; b--; } } } stack<char> st; for (i = 0; i < n; i++) { if (ar[i] == '(') st.push(ar[i]); else { if (st.size() > 0) st.pop(); } if (st.size() == 0 && i < n - 1) { cout << ":("; return 0; } } if (st.size() == 0) { cout << ar; } else { cout << ":("; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-ffloat-store") #pragma GCC optimize("-fno-defer-pop") long long int power(long long int a, long long int b, long long int m) { if (b == 0) return 1; if (b == 1) return a % m; long long int t = power(a, b / 2, m) % m; t = (t * t) % m; if (b & 1) t = ((t % m) * (a % m)) % m; return t; } long long int modInverse(long long int a, long long int m) { return power(a, m - 2, m); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int i, j, k, l, n; cin >> n; char ar[n + 1]; cin >> ar; if (n % 2 == 1) { cout << ":("; return 0; } long long int a = 0, b = 0, c = 0; for (i = 0; i < n; i++) { if (ar[i] == '?') c++; else if (ar[i] == '(') a++; else b++; } a = n / 2 - a; b = n / 2 - b; for (i = 0; i < n; i++) { if (ar[i] == '?') { if (a > 0) { ar[i] = '('; a--; } else { ar[i] = ')'; b--; } } } stack<char> st; for (i = 0; i < n; i++) { if (ar[i] == '(') st.push(ar[i]); else { if (st.size() > 0) st.pop(); } if (st.size() == 0 && i < n - 1) { cout << ":("; return 0; } } if (st.size() == 0) { cout << ar; } else { cout << ":("; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; long long k = 0, kol1 = 0; cin >> k; cin >> s; if (!(s[0] == ')') and !(s.size() >= 3 and (s[s.size() - 2] == '('))) { vector<int> ar(s.size(), 0); long long kolx = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') { ar[i] = 1; kol1++; } if (s[i] == ')') { ar[i] = -1; kol1--; } if (s[i] == '?') { kolx++; } } if (s[0] == '?') { ar[0] = 1; kol1++; kolx--; } for (int i = 0; i < s.size(); i++) { if (ar[i] == 0) { if (kolx == kol1) { ar[i] = -1; kolx--; kol1--; } else { ar[i] = 1; kol1++; kolx--; } } } bool b1 = true; string ans = ""; kol1 = 0; for (int i = 0; i < s.size(); i++) { if (ar[i] == 1) { ans += '('; kol1++; } else { ans += ')'; kol1--; } if (i != s.size() - 1 and kol1 <= 0) { b1 = false; } } if (!kol1 and b1) { cout << ans; } else { cout << ":("; } } else { cout << ":("; } return 0; }
### Prompt Please create a solution in CPP to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; long long k = 0, kol1 = 0; cin >> k; cin >> s; if (!(s[0] == ')') and !(s.size() >= 3 and (s[s.size() - 2] == '('))) { vector<int> ar(s.size(), 0); long long kolx = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') { ar[i] = 1; kol1++; } if (s[i] == ')') { ar[i] = -1; kol1--; } if (s[i] == '?') { kolx++; } } if (s[0] == '?') { ar[0] = 1; kol1++; kolx--; } for (int i = 0; i < s.size(); i++) { if (ar[i] == 0) { if (kolx == kol1) { ar[i] = -1; kolx--; kol1--; } else { ar[i] = 1; kol1++; kolx--; } } } bool b1 = true; string ans = ""; kol1 = 0; for (int i = 0; i < s.size(); i++) { if (ar[i] == 1) { ans += '('; kol1++; } else { ans += ')'; kol1--; } if (i != s.size() - 1 and kol1 <= 0) { b1 = false; } } if (!kol1 and b1) { cout << ans; } else { cout << ":("; } } else { cout << ":("; } return 0; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; int n, m, h; bool c[1999][1000]; int res[1000][1000], a[1000], b[1000]; int main() { cin >> n; if (n % 2 == 1) { cout << ":("; return 0; } string s; cin >> s; int m = 0; int q = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') m++; else if (s[i] == ')') m--; else q++; } if (q < abs(m)) { cout << ":("; return 0; } int i = 0; while (i < n) { while (s[i] != '?') { i++; if (i == n) break; } if (i == n) break; if (q > m) { s[i] = '('; q--; m++; } else { s[i] = ')'; q--; m--; } if (q <= 0) break; } m = 1; if (s[0] == ')') { cout << ":("; return 0; } for (int i = 1; i < n; i++) { if (s[i] == '(') m++; else m--; if (m == 0 && i < n - 1) { cout << ":("; return 0; } } cout << s << endl; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; int n, m, h; bool c[1999][1000]; int res[1000][1000], a[1000], b[1000]; int main() { cin >> n; if (n % 2 == 1) { cout << ":("; return 0; } string s; cin >> s; int m = 0; int q = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') m++; else if (s[i] == ')') m--; else q++; } if (q < abs(m)) { cout << ":("; return 0; } int i = 0; while (i < n) { while (s[i] != '?') { i++; if (i == n) break; } if (i == n) break; if (q > m) { s[i] = '('; q--; m++; } else { s[i] = ')'; q--; m--; } if (q <= 0) break; } m = 1; if (s[0] == ')') { cout << ":("; return 0; } for (int i = 1; i < n; i++) { if (s[i] == '(') m++; else m--; if (m == 0 && i < n - 1) { cout << ":("; return 0; } } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; string s; cin >> n >> s; int su = 0, st = 0, fi = 0; for (int i = 0; i < n; i++) if (s[i] == '(') su++; else if (s[i] == ')') su--; else fi++; int a = (su + fi) / 2; int b = fi - a; if (a - b != su || a < 0 || b < 0) { cout << ":(\n"; return 0; } for (int i = 0; i < n; i++) { if (s[i] == '(') st++; else if (s[i] == ')') st--; else if (s[i] == '?') { if (b > 0) { s[i] = '('; b--; st++; } else { s[i] = ')'; st--; } } if (st <= 0 && i < n - 1) { cout << ":(\n"; return 0; } } cout << s << "\n"; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; string s; cin >> n >> s; int su = 0, st = 0, fi = 0; for (int i = 0; i < n; i++) if (s[i] == '(') su++; else if (s[i] == ')') su--; else fi++; int a = (su + fi) / 2; int b = fi - a; if (a - b != su || a < 0 || b < 0) { cout << ":(\n"; return 0; } for (int i = 0; i < n; i++) { if (s[i] == '(') st++; else if (s[i] == ')') st--; else if (s[i] == '?') { if (b > 0) { s[i] = '('; b--; st++; } else { s[i] = ')'; st--; } } if (st <= 0 && i < n - 1) { cout << ":(\n"; return 0; } } cout << s << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string str; cin >> str; int open = 0, close = 0, remaining = 0; int s = 0; for (int i = 0; i < str.size(); i++) { if (str[i] == '(') { open++; } else if (str[i] == ')') { close++; } else remaining++; } if (remaining == 0) { for (int i = 0; i < str.size() - 1; i++) { if (str[i] == '(') { s++; } else { s--; } if (s <= 0) { cout << ":(" << endl; return 0; } } if (str[str.size() - 1] == '(') s++; else s--; if (s == 0) cout << str << endl; else cout << ":(" << endl; return 0; } if (n % 2 != 0) { cout << ":(" << endl; return 0; } int a = (open - close >= 0) ? 0 : close - open; int b = (close - open >= 0) ? 0 : open - close; remaining -= (a + b); if (remaining < 0 || remaining % 2 != 0) { cout << ":(" << endl; return 0; } a += remaining / 2; b += remaining / 2; int i = 0; string ans = ""; while (a > 0) { if (str[i] == '?') { ans += '('; a--; } else { ans += str[i]; } i++; } while (b > 0) { if (str[i] == '?') { ans += ')'; b--; } else { ans += str[i]; } i++; } while (i < str.size()) { ans += str[i]; i++; } int sum = 0; for (int i = 0; i < ans.size() - 1; i++) { if (ans[i] == '(') sum++; else sum--; if (sum <= 0) { cout << ":(" << endl; return 0; } } if (ans[ans.size() - 1] == '(') sum++; else sum--; if (sum == 0) cout << ans; else cout << ":(" << endl; }
### Prompt Generate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string str; cin >> str; int open = 0, close = 0, remaining = 0; int s = 0; for (int i = 0; i < str.size(); i++) { if (str[i] == '(') { open++; } else if (str[i] == ')') { close++; } else remaining++; } if (remaining == 0) { for (int i = 0; i < str.size() - 1; i++) { if (str[i] == '(') { s++; } else { s--; } if (s <= 0) { cout << ":(" << endl; return 0; } } if (str[str.size() - 1] == '(') s++; else s--; if (s == 0) cout << str << endl; else cout << ":(" << endl; return 0; } if (n % 2 != 0) { cout << ":(" << endl; return 0; } int a = (open - close >= 0) ? 0 : close - open; int b = (close - open >= 0) ? 0 : open - close; remaining -= (a + b); if (remaining < 0 || remaining % 2 != 0) { cout << ":(" << endl; return 0; } a += remaining / 2; b += remaining / 2; int i = 0; string ans = ""; while (a > 0) { if (str[i] == '?') { ans += '('; a--; } else { ans += str[i]; } i++; } while (b > 0) { if (str[i] == '?') { ans += ')'; b--; } else { ans += str[i]; } i++; } while (i < str.size()) { ans += str[i]; i++; } int sum = 0; for (int i = 0; i < ans.size() - 1; i++) { if (ans[i] == '(') sum++; else sum--; if (sum <= 0) { cout << ":(" << endl; return 0; } } if (ans[ans.size() - 1] == '(') sum++; else sum--; if (sum == 0) cout << ans; else cout << ":(" << endl; } ```
#include <bits/stdc++.h> char s[300000 + 5]; int n; int main() { scanf("%d", &n); scanf("%s", s + 1); if (n & 1) { puts(":("); return 0; } if (s[n] == '?') { s[n] = ')'; } if (s[1] == '?') { s[1] = '('; } if (s[1] == ')' || s[n] == '(') { puts(":("); return 0; } int sum = 0; int total = 0; for (int i = 2; i < n; i++) { if (s[i] == ')') { total++; } } for (int i = n - 1; i > 1; i--) { if (s[i] == '?') { if (total < ((n - 2) >> 1)) { total++; s[i] = ')'; sum++; } else { s[i] = '('; sum--; } } else { if (s[i] == ')') { sum++; } else { sum--; if (sum < 0) { puts(":("); return 0; } } } } if (sum > 0) { puts(":("); return 0; } sum = 0; for (int i = 2; i < n; i++) { if (s[i] == ')') { sum--; } else { sum++; } if (sum < 0) { puts(":("); return 0; } } puts(s + 1); return 0; }
### Prompt Develop a solution in CPP to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> char s[300000 + 5]; int n; int main() { scanf("%d", &n); scanf("%s", s + 1); if (n & 1) { puts(":("); return 0; } if (s[n] == '?') { s[n] = ')'; } if (s[1] == '?') { s[1] = '('; } if (s[1] == ')' || s[n] == '(') { puts(":("); return 0; } int sum = 0; int total = 0; for (int i = 2; i < n; i++) { if (s[i] == ')') { total++; } } for (int i = n - 1; i > 1; i--) { if (s[i] == '?') { if (total < ((n - 2) >> 1)) { total++; s[i] = ')'; sum++; } else { s[i] = '('; sum--; } } else { if (s[i] == ')') { sum++; } else { sum--; if (sum < 0) { puts(":("); return 0; } } } } if (sum > 0) { puts(":("); return 0; } sum = 0; for (int i = 2; i < n; i++) { if (s[i] == ')') { sum--; } else { sum++; } if (sum < 0) { puts(":("); return 0; } } puts(s + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long const N = 5 * 1e5 + 5; long long x1, r, x2, c, q, y, l, e, y2, d, ans, a[N], aaa[N], b, y3, k, pref[5005][4]; long long m, n; bool us[N], uss[N]; char z, x; vector<long long> v; set<long long> st; string t, second, ss; pair<long long, long long> p[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> second; if (n % 2 != 0 || second[n - 1] == '(' || second[0] == ')') return cout << ":(", 0; for (int i = 0; i < n; i++) { if (second[i] == '(') q++; } if (q > n / 2) return cout << ":(", 0; for (int i = 0; i < n; i++) { if (second[i] == '?' && q < n / 2) q++, second[i] = '('; else if (second[i] == '?') second[i] = ')'; } for (int i = 0; i < n - 1; i++) { if (second[i] == '(') k++; else k--; if (k <= 0) return cout << ":(", 0; } k--; if (k != 0) cout << ":("; else cout << second; }
### Prompt Please create a solution in CPP to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long const N = 5 * 1e5 + 5; long long x1, r, x2, c, q, y, l, e, y2, d, ans, a[N], aaa[N], b, y3, k, pref[5005][4]; long long m, n; bool us[N], uss[N]; char z, x; vector<long long> v; set<long long> st; string t, second, ss; pair<long long, long long> p[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> second; if (n % 2 != 0 || second[n - 1] == '(' || second[0] == ')') return cout << ":(", 0; for (int i = 0; i < n; i++) { if (second[i] == '(') q++; } if (q > n / 2) return cout << ":(", 0; for (int i = 0; i < n; i++) { if (second[i] == '?' && q < n / 2) q++, second[i] = '('; else if (second[i] == '?') second[i] = ')'; } for (int i = 0; i < n - 1; i++) { if (second[i] == '(') k++; else k--; if (k <= 0) return cout << ":(", 0; } k--; if (k != 0) cout << ":("; else cout << second; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5; char s[maxn]; int now = 0, len, fr = 1e9, cnt[maxn], val[maxn]; int main() { scanf("%d\n", &len); gets(s); for (int i = len - 1; i >= 0; i--) { cnt[i] = cnt[i + 1]; if (s[i] == ')') { val[i] = val[i + 1] + 1; } if (s[i] == '(') { val[i] = val[i + 1] - 1; } if (s[i] == '?') { val[i] = val[i + 1]; cnt[i]++; } } for (int i = 0; i <= len - 1; i++) { if (now >= 0 && i) { fr = min(fr, i); } if (s[i] == ')') { now++; continue; } if (s[i] == '(') { now--; continue; } if (s[i] == '?') { if (val[i] + now > -cnt[i + 1]) { now--; s[i] = '('; } else { now++; s[i] = ')'; } } } if (now || fr != 1e9) { printf(":(\n"); return 0; } for (int i = 0; i < len; i++) { printf("%c", s[i]); } printf("\n"); return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 5e5; char s[maxn]; int now = 0, len, fr = 1e9, cnt[maxn], val[maxn]; int main() { scanf("%d\n", &len); gets(s); for (int i = len - 1; i >= 0; i--) { cnt[i] = cnt[i + 1]; if (s[i] == ')') { val[i] = val[i + 1] + 1; } if (s[i] == '(') { val[i] = val[i + 1] - 1; } if (s[i] == '?') { val[i] = val[i + 1]; cnt[i]++; } } for (int i = 0; i <= len - 1; i++) { if (now >= 0 && i) { fr = min(fr, i); } if (s[i] == ')') { now++; continue; } if (s[i] == '(') { now--; continue; } if (s[i] == '?') { if (val[i] + now > -cnt[i + 1]) { now--; s[i] = '('; } else { now++; s[i] = ')'; } } } if (now || fr != 1e9) { printf(":(\n"); return 0; } for (int i = 0; i < len; i++) { printf("%c", s[i]); } printf("\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n, c1 = 0, c2 = 0, c = 0; cin >> n; string s; cin >> s; for (long long i = 0; i < n; i++) { if (s[i] == '(') c1++; else if (s[i] == ')') c2++; } if (n % 2 != 0) { cout << ":("; return 0; } c1 = (n / 2) - c1; c2 = (n / 2) - c2; for (long long i = 0; i < n; i++) { if (s[i] == '?' && c1 > 0) { s[i] = '('; c1--; } else if (s[i] == '?' && c2 > 0) { s[i] = ')'; c2--; } } for (long long i = 0; i < n - 1; i++) { if (s[i] == '(') c++; else c--; if (c == 0) { cout << ":("; return 0; } } c = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') c++; else c--; if (c < 0) { cout << ":("; return 0; } } if (c != 0) { cout << ":("; return 0; } cout << s; }
### Prompt Develop a solution in CPP to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n, c1 = 0, c2 = 0, c = 0; cin >> n; string s; cin >> s; for (long long i = 0; i < n; i++) { if (s[i] == '(') c1++; else if (s[i] == ')') c2++; } if (n % 2 != 0) { cout << ":("; return 0; } c1 = (n / 2) - c1; c2 = (n / 2) - c2; for (long long i = 0; i < n; i++) { if (s[i] == '?' && c1 > 0) { s[i] = '('; c1--; } else if (s[i] == '?' && c2 > 0) { s[i] = ')'; c2--; } } for (long long i = 0; i < n - 1; i++) { if (s[i] == '(') c++; else c--; if (c == 0) { cout << ":("; return 0; } } c = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') c++; else c--; if (c < 0) { cout << ":("; return 0; } } if (c != 0) { cout << ":("; return 0; } cout << s; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; char a[maxn]; int main() { int n; scanf("%d%s", &n, a + 1); if (n % 2 == 1) { printf(":(\n"); return 0; } int s1 = 0, s2 = 0; for (int i = 1; i <= n; i++) { if (a[i] == '(') s1++; else if (a[i] == ')') s2++; } int n1 = n / 2 - s1; int n2 = n / 2 - s2; for (int i = 1; i <= n; i++) { if (a[i] == '?' && n1 > 0) { a[i] = '('; n1--; } else if (a[i] == '?') { a[i] = ')'; n2--; } } int sum = 0; int f = 1; for (int i = 1; i < n; i++) { if (a[i] == '(') sum++; else sum--; if (sum <= 0) { f = 0; break; } } if (a[n] == ')') sum--; if (sum != 0) f = 0; if (f) { printf("%s\n", a + 1); } else printf(":(\n"); }
### Prompt Your challenge is to write a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; char a[maxn]; int main() { int n; scanf("%d%s", &n, a + 1); if (n % 2 == 1) { printf(":(\n"); return 0; } int s1 = 0, s2 = 0; for (int i = 1; i <= n; i++) { if (a[i] == '(') s1++; else if (a[i] == ')') s2++; } int n1 = n / 2 - s1; int n2 = n / 2 - s2; for (int i = 1; i <= n; i++) { if (a[i] == '?' && n1 > 0) { a[i] = '('; n1--; } else if (a[i] == '?') { a[i] = ')'; n2--; } } int sum = 0; int f = 1; for (int i = 1; i < n; i++) { if (a[i] == '(') sum++; else sum--; if (sum <= 0) { f = 0; break; } } if (a[n] == ')') sum--; if (sum != 0) f = 0; if (f) { printf("%s\n", a + 1); } else printf(":(\n"); } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); string s; cin >> s; if (n % 2) { printf(":(\n"); return 0; } int cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt++; } for (int i = 0; i < n; i++) { if (s[i] == '?') { if (cnt < n / 2) { s[i] = '('; cnt++; } else s[i] = ')'; } } cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt++; else cnt--; if (cnt < 0 || (cnt == 0 && i != n - 1)) { printf(":(\n"); return 0; } } if (cnt) printf(":(\n"); else cout << s << endl; return 0; }
### Prompt Generate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); string s; cin >> s; if (n % 2) { printf(":(\n"); return 0; } int cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt++; } for (int i = 0; i < n; i++) { if (s[i] == '?') { if (cnt < n / 2) { s[i] = '('; cnt++; } else s[i] = ')'; } } cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt++; else cnt--; if (cnt < 0 || (cnt == 0 && i != n - 1)) { printf(":(\n"); return 0; } } if (cnt) printf(":(\n"); else cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; if (n % 2) { cout << ":(" << endl; return 0; } string s; cin >> s; long long t1 = n / 2; long long cnt = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') { cnt++; } } if (cnt > t1) { cout << ":(" << endl; return 0; } t1 -= cnt; for (long long i = 0; i < n; i++) { if (s[i] == '?') { if (t1 > 0) { s[i] = '('; t1--; } else { s[i] = ')'; } } } long long cur = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') { cur++; } else if (s[i] == ')') { cur--; } if (cur < 0) { cout << ":(" << endl; return 0; } else if (i != n - 1 && cur == 0) { cout << ":(" << endl; return 0; } else if (i == n - 1 && cur != 0) { cout << ":(" << endl; return 0; } } cout << s << endl; }
### Prompt In CPP, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; if (n % 2) { cout << ":(" << endl; return 0; } string s; cin >> s; long long t1 = n / 2; long long cnt = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') { cnt++; } } if (cnt > t1) { cout << ":(" << endl; return 0; } t1 -= cnt; for (long long i = 0; i < n; i++) { if (s[i] == '?') { if (t1 > 0) { s[i] = '('; t1--; } else { s[i] = ')'; } } } long long cur = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') { cur++; } else if (s[i] == ')') { cur--; } if (cur < 0) { cout << ":(" << endl; return 0; } else if (i != n - 1 && cur == 0) { cout << ":(" << endl; return 0; } else if (i == n - 1 && cur != 0) { cout << ":(" << endl; return 0; } } cout << s << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; int fp = 0, bp = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') fp++; else if (s[i] == ')') bp++; } int tm = min(fp, bp); fp = fp - tm; bp = bp - tm; int tbd = 0; for (int i = 0; i < n && bp != 0; i++) { if (s[i] == '?') { s[i] = '('; bp--; } } for (int i = n - 1; i >= 0 && fp != 0; i--) { if (s[i] == '?') { s[i] = ')'; fp--; } } for (int i = 0; i < n; i++) { if (s[i] == '?') tbd++; } if (tbd % 2 != 0) { cout << ":(" << endl; return 0; } tbd = tbd / 2; int p = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == '(') p++; else if (s[i] == ')') p--; else { if (tbd > 0) { s[i] = '('; tbd--; p++; } else { s[i] = ')'; p--; } } if (p <= 0) { cout << ":(" << endl; return 0; } } if (p == 1 && s[n - 1] == ')') { cout << s << endl; } else { cout << ":(" << endl; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; int fp = 0, bp = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') fp++; else if (s[i] == ')') bp++; } int tm = min(fp, bp); fp = fp - tm; bp = bp - tm; int tbd = 0; for (int i = 0; i < n && bp != 0; i++) { if (s[i] == '?') { s[i] = '('; bp--; } } for (int i = n - 1; i >= 0 && fp != 0; i--) { if (s[i] == '?') { s[i] = ')'; fp--; } } for (int i = 0; i < n; i++) { if (s[i] == '?') tbd++; } if (tbd % 2 != 0) { cout << ":(" << endl; return 0; } tbd = tbd / 2; int p = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == '(') p++; else if (s[i] == ')') p--; else { if (tbd > 0) { s[i] = '('; tbd--; p++; } else { s[i] = ')'; p--; } } if (p <= 0) { cout << ":(" << endl; return 0; } } if (p == 1 && s[n - 1] == ')') { cout << s << endl; } else { cout << ":(" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; string v; while (cin >> n) { cin >> v; stack<char> s; bool f = true; if (n % 2 == 1) { cout << ":(" << endl; return 0; } int count = 0; for (int i = 0; i < n; i++) { if (v[i] == '(') count++; } for (int i = 0; i < n; i++) { if (v[i] == '?') { if (count < n / 2) { v[i] = '('; count++; } else v[i] = ')'; } if (v[i] == '(') { s.push('('); } else { if (s.empty()) { f = false; break; } else { s.pop(); if (s.empty() && i != n - 1) { f = false; break; } } } } if (f && s.empty()) { cout << v << endl; } else cout << ":(" << endl; } }
### Prompt Develop a solution in Cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; string v; while (cin >> n) { cin >> v; stack<char> s; bool f = true; if (n % 2 == 1) { cout << ":(" << endl; return 0; } int count = 0; for (int i = 0; i < n; i++) { if (v[i] == '(') count++; } for (int i = 0; i < n; i++) { if (v[i] == '?') { if (count < n / 2) { v[i] = '('; count++; } else v[i] = ')'; } if (v[i] == '(') { s.push('('); } else { if (s.empty()) { f = false; break; } else { s.pop(); if (s.empty() && i != n - 1) { f = false; break; } } } } if (f && s.empty()) { cout << v << endl; } else cout << ":(" << endl; } } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") using namespace std; template <typename T> void readv(vector<T> &v, int x) { v = vector<T>(x); for (int(i) = int(0); i < int(x); ++i) cin >> (v[i]); } int n; string s; void _input() { cin >> n >> s; } bool solve() { if (n % 2 or s[0] == ')') return false; else { int ba = 0; int op, cl, du; op = cl = du = 0; int xb = -1e6, nb = 1e6; for (int(i) = int(0); i < int(n); ++i) { if (s[i] == '(') { ++op, ++ba; } else if (s[i] == ')') { ++cl, --ba; } else ++du; xb = max(xb, ba); nb = min(nb, ba); } if (op > n / 2 or cl > n / 2) return false; op = n / 2 - op, cl = n / 2 - cl; int cb = 0; for (int(i) = int(0); i < int(n); ++i) { if (s[i] == '(') { ++cb; } else if (s[i] == ')') { --cb; } else { if (op) { s[i] = '('; --op; ++cb; } else if (cl) { s[i] = ')'; --cl; --cb; } else { cerr << "fail " << i << "\n"; } } if (i != n - 1 and cb == 0) return false; } cout << s << endl; } return true; } void _solve() { string fa = ":(\n"; if (!solve()) cout << fa; } int main() { double st = clock(); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int qq = 1; int cc = 0; while (cc++ < qq) { _input(); _solve(); } cerr << 1000.0 * (1.0 * clock() - st) / (1.0 * CLOCKS_PER_SEC) << " ms ellapsed\n"; }
### Prompt Your challenge is to write a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") using namespace std; template <typename T> void readv(vector<T> &v, int x) { v = vector<T>(x); for (int(i) = int(0); i < int(x); ++i) cin >> (v[i]); } int n; string s; void _input() { cin >> n >> s; } bool solve() { if (n % 2 or s[0] == ')') return false; else { int ba = 0; int op, cl, du; op = cl = du = 0; int xb = -1e6, nb = 1e6; for (int(i) = int(0); i < int(n); ++i) { if (s[i] == '(') { ++op, ++ba; } else if (s[i] == ')') { ++cl, --ba; } else ++du; xb = max(xb, ba); nb = min(nb, ba); } if (op > n / 2 or cl > n / 2) return false; op = n / 2 - op, cl = n / 2 - cl; int cb = 0; for (int(i) = int(0); i < int(n); ++i) { if (s[i] == '(') { ++cb; } else if (s[i] == ')') { --cb; } else { if (op) { s[i] = '('; --op; ++cb; } else if (cl) { s[i] = ')'; --cl; --cb; } else { cerr << "fail " << i << "\n"; } } if (i != n - 1 and cb == 0) return false; } cout << s << endl; } return true; } void _solve() { string fa = ":(\n"; if (!solve()) cout << fa; } int main() { double st = clock(); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int qq = 1; int cc = 0; while (cc++ < qq) { _input(); _solve(); } cerr << 1000.0 * (1.0 * clock() - st) / (1.0 * CLOCKS_PER_SEC) << " ms ellapsed\n"; } ```
#include <bits/stdc++.h> using namespace std; void test_case() { long long int n; cin >> n; string s1; cin >> s1; if (s1[0] == ')' || s1[n - 1] == '(' || n % 2 == 1) { cout << ":(" << endl; return; } string s = s1; s[0] = '('; s[n - 1] = ')'; long long int d = 0; for (long long int i = 1; i < n - 1; i++) { if (s[i] == '(') { d++; continue; } else if (s[i] == ')') { d--; while (d < 0) { long long int j = i; while (j > 0 && !(s[j] == ')' && s1[j] == '?')) j--; if (j == 0) { cout << ":(" << endl; return; } s[j] = '('; d += 2; } continue; } if (d == 0) { s[i] = '('; d++; } else if (d > 0) { s[i] = ')'; d--; } } if (d != 0) { cout << ":(" << endl; return; } cout << s << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; while (t--) test_case(); return 0; }
### Prompt Create a solution in cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; void test_case() { long long int n; cin >> n; string s1; cin >> s1; if (s1[0] == ')' || s1[n - 1] == '(' || n % 2 == 1) { cout << ":(" << endl; return; } string s = s1; s[0] = '('; s[n - 1] = ')'; long long int d = 0; for (long long int i = 1; i < n - 1; i++) { if (s[i] == '(') { d++; continue; } else if (s[i] == ')') { d--; while (d < 0) { long long int j = i; while (j > 0 && !(s[j] == ')' && s1[j] == '?')) j--; if (j == 0) { cout << ":(" << endl; return; } s[j] = '('; d += 2; } continue; } if (d == 0) { s[i] = '('; d++; } else if (d > 0) { s[i] = ')'; d--; } } if (d != 0) { cout << ":(" << endl; return; } cout << s << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; while (t--) test_case(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; string s; cin >> n >> s; if (n & 1) { cout << ":(\n"; return 0; } int cur = 0, c = 0; bool bad = false; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') cur++; if (s[i] == ')') cur--; if (s[i] == '?') c++; } for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { if (cur == c) s[i] = ')', --cur, --c; else s[i] = '(', ++cur, --c; } } if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; cur = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') cur++; if (s[i] == ')') cur--; if (cur < 0) bad = true; } if (cur) bad = true; if (s[0] != '(' || s[n - 1] != ')' || bad) { cout << ":(\n"; return 0; } cout << s << "\n"; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; string s; cin >> n >> s; if (n & 1) { cout << ":(\n"; return 0; } int cur = 0, c = 0; bool bad = false; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') cur++; if (s[i] == ')') cur--; if (s[i] == '?') c++; } for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { if (cur == c) s[i] = ')', --cur, --c; else s[i] = '(', ++cur, --c; } } if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; cur = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') cur++; if (s[i] == ')') cur--; if (cur < 0) bad = true; } if (cur) bad = true; if (s[0] != '(' || s[n - 1] != ')' || bad) { cout << ":(\n"; return 0; } cout << s << "\n"; return 0; } ```
#include <bits/stdc++.h> template <class C, class E> inline bool contains(const C& container, const E& element) { return container.find(element) != container.end(); } template <class T> inline void checkmin(T& a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T& a, T b) { if (b > a) a = b; } using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << std::setprecision(15); cout << std::fixed; int n; cin >> n; string s; cin >> s; int tb = 0; if (s[0] == ')' || s[n - 1] == '(') { cout << ":("; return 0; } if (n % 2 == 1) { cout << ":("; return 0; } s[0] = '('; s[n - 1] = ')'; int mis = 0; int totCC = 0; for (char c : s) { if (c == '(') { totCC++; tb++; } else if (c == ')') tb--; else mis++; } int canPut = n / 2 - totCC; if (canPut < 0) { cout << ":("; return 0; } int tt = 1; for (int i = (1), _b = (n - 2); i <= _b; i++) { char& c = s[i]; if (c == '?') { if (canPut > 0) { c = '('; canPut--; } else c = ')'; } if (c == '(') { tt++; } else if (c == ')') tt--; if (tt == 0) { cout << ":("; return 0; } } tt--; if (tt != 0) { cout << ":("; return 0; } cout << s; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> template <class C, class E> inline bool contains(const C& container, const E& element) { return container.find(element) != container.end(); } template <class T> inline void checkmin(T& a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T& a, T b) { if (b > a) a = b; } using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << std::setprecision(15); cout << std::fixed; int n; cin >> n; string s; cin >> s; int tb = 0; if (s[0] == ')' || s[n - 1] == '(') { cout << ":("; return 0; } if (n % 2 == 1) { cout << ":("; return 0; } s[0] = '('; s[n - 1] = ')'; int mis = 0; int totCC = 0; for (char c : s) { if (c == '(') { totCC++; tb++; } else if (c == ')') tb--; else mis++; } int canPut = n / 2 - totCC; if (canPut < 0) { cout << ":("; return 0; } int tt = 1; for (int i = (1), _b = (n - 2); i <= _b; i++) { char& c = s[i]; if (c == '?') { if (canPut > 0) { c = '('; canPut--; } else c = ')'; } if (c == '(') { tt++; } else if (c == ')') tt--; if (tt == 0) { cout << ":("; return 0; } } tt--; if (tt != 0) { cout << ":("; return 0; } cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int tot = 0, cnt = 0; for (auto x : s) { if (x == ')') tot--; if (x == '(') tot++; if (x == '?') cnt++; } if (abs(cnt) % 2 != abs(tot) % 2 || cnt < tot) { cout << ":("; return 0; } int open = (cnt - tot) / 2, close = cnt - open; if (open < 0 || close < 0) { cout << ":("; return 0; }; int cur = 0; string ss = ""; for (auto x : s) if (x == '?') { if (open) { ss += '('; open--; } else ss += ')'; } else ss += x; for (int i = 0; i < n; ++i) { char x = ss[i]; cur += (x == '(' ? 1 : -1); if (cur <= 0 && i + 1 != n) { cout << ":("; return 0; } } if (cur != 0) { cout << ":("; return 0; }; cout << ss; return 0; }
### Prompt Create a solution in cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int tot = 0, cnt = 0; for (auto x : s) { if (x == ')') tot--; if (x == '(') tot++; if (x == '?') cnt++; } if (abs(cnt) % 2 != abs(tot) % 2 || cnt < tot) { cout << ":("; return 0; } int open = (cnt - tot) / 2, close = cnt - open; if (open < 0 || close < 0) { cout << ":("; return 0; }; int cur = 0; string ss = ""; for (auto x : s) if (x == '?') { if (open) { ss += '('; open--; } else ss += ')'; } else ss += x; for (int i = 0; i < n; ++i) { char x = ss[i]; cur += (x == '(' ? 1 : -1); if (cur <= 0 && i + 1 != n) { cout << ":("; return 0; } } if (cur != 0) { cout << ":("; return 0; }; cout << ss; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<char> s(n); for (int i = 0; i < n; i++) { cin >> s[i]; } if (n % 2 != 0 || s[0] == ')' || s[n - 1] == '(') { cout << ":("; return 0; } s[0] = '('; s[n - 1] = ')'; map<char, int> m, need; m['?'] = 0; m[')'] = 0; need[')'] = 0; m['('] = 0; need['('] = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') { m['(']++; need[')']++; need['(']--; } if (s[i] == ')') { m[')']++; need['(']++; need[')']--; } if (s[i] == '?') { m['?']++; } } if (need['('] < 0) need['('] = 0; if (need[')'] < 0) need[')'] = 0; if ((m['?'] - m[')'] - m['(']) % 2 != 0) { cout << ":("; return 0; } int col_1 = need['('] + (m['?'] - need[')'] - need['(']) / 2; int col_2 = need[')'] + (m['?'] - need[')'] - need['(']) / 2; for (int i = 1; i < n - 1 && col_1 != 0; i++) { if (s[i] == '?') { s[i] = '('; col_1--; } } for (int i = n - 2; i >= 1 && col_2 != 0; i--) { if (s[i] == '?') { s[i] = ')'; col_2--; } } int depth = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == '(') { depth++; } if (s[i] == ')') { depth--; } if (depth <= 0) { cout << ":("; return 0; } } depth--; if (depth != 0) { cout << ":("; return 0; } for (int i = 0; i < n; i++) { cout << s[i]; } return 0; }
### Prompt In CPP, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<char> s(n); for (int i = 0; i < n; i++) { cin >> s[i]; } if (n % 2 != 0 || s[0] == ')' || s[n - 1] == '(') { cout << ":("; return 0; } s[0] = '('; s[n - 1] = ')'; map<char, int> m, need; m['?'] = 0; m[')'] = 0; need[')'] = 0; m['('] = 0; need['('] = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') { m['(']++; need[')']++; need['(']--; } if (s[i] == ')') { m[')']++; need['(']++; need[')']--; } if (s[i] == '?') { m['?']++; } } if (need['('] < 0) need['('] = 0; if (need[')'] < 0) need[')'] = 0; if ((m['?'] - m[')'] - m['(']) % 2 != 0) { cout << ":("; return 0; } int col_1 = need['('] + (m['?'] - need[')'] - need['(']) / 2; int col_2 = need[')'] + (m['?'] - need[')'] - need['(']) / 2; for (int i = 1; i < n - 1 && col_1 != 0; i++) { if (s[i] == '?') { s[i] = '('; col_1--; } } for (int i = n - 2; i >= 1 && col_2 != 0; i--) { if (s[i] == '?') { s[i] = ')'; col_2--; } } int depth = 0; for (int i = 0; i < n - 1; i++) { if (s[i] == '(') { depth++; } if (s[i] == ')') { depth--; } if (depth <= 0) { cout << ":("; return 0; } } depth--; if (depth != 0) { cout << ":("; return 0; } for (int i = 0; i < n; i++) { cout << s[i]; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int len; string store; bool matched[300005]; int main() { ios::sync_with_stdio(false); cin >> len; cin >> store; if (len % 2) { cout << ":(\n"; return 0; } int n1 = 0, n2 = 0; int temp = 0; for (char c : store) { if (c == '(') n1++; if (c == ')') n2++; } if (n1 > len / 2 || n2 > len / 2) { cout << ":(\n"; return 0; } int num = len / 2 - n1; for (char &c : store) { if (c == '?') { if (num) { c = '('; num--; } else c = ')'; } } bool safe = true; int pos = 0, stc = 0; ; while (pos < len - 1) { if (store[pos] == '(') stc++; else { stc--; if (stc <= 0) { safe = false; break; } } pos++; } if (safe && stc == 1) { cout << store << endl; } else { cout << ":(\n"; } return 0; }
### Prompt Generate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int len; string store; bool matched[300005]; int main() { ios::sync_with_stdio(false); cin >> len; cin >> store; if (len % 2) { cout << ":(\n"; return 0; } int n1 = 0, n2 = 0; int temp = 0; for (char c : store) { if (c == '(') n1++; if (c == ')') n2++; } if (n1 > len / 2 || n2 > len / 2) { cout << ":(\n"; return 0; } int num = len / 2 - n1; for (char &c : store) { if (c == '?') { if (num) { c = '('; num--; } else c = ')'; } } bool safe = true; int pos = 0, stc = 0; ; while (pos < len - 1) { if (store[pos] == '(') stc++; else { stc--; if (stc <= 0) { safe = false; break; } } pos++; } if (safe && stc == 1) { cout << store << endl; } else { cout << ":(\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long N, t = 0; cin >> N; string S; cin >> S; for (long long i = 0; i < N; i++) if (S[i] == '(') t++; long long a = 0; for (long long i = 0; i < N; i++) { if (S[i] == '?') { if (2 * t < N) S[i] = '(', t++; else S[i] = ')'; } if (S[i] == '(') a++; if ((i != N - 1 && 2 * a <= i + 1) || (i == N - 1 && 2 * a != i + 1)) return cout << ":(" && 0; } cout << S << endl; }
### Prompt Please create a solution in CPP to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long N, t = 0; cin >> N; string S; cin >> S; for (long long i = 0; i < N; i++) if (S[i] == '(') t++; long long a = 0; for (long long i = 0; i < N; i++) { if (S[i] == '?') { if (2 * t < N) S[i] = '(', t++; else S[i] = ')'; } if (S[i] == '(') a++; if ((i != N - 1 && 2 * a <= i + 1) || (i == N - 1 && 2 * a != i + 1)) return cout << ":(" && 0; } cout << S << endl; } ```
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; bool isPrime(int num) { if (num == 0) return false; bool flag = true; for (int i = 2; i <= sqrt(num); i++) { if (num % i == 0) { flag = false; break; } } return flag; } long long int powermodm(long long int x, long long int n, long long int M) { long long int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } long long int power(long long int _a, long long int _b) { long long int _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a); _b /= 2; _a = (_a * _a); } return _r; } long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } long long int lcm(long long int a, long long int b) { return (max(a, b) / gcd(a, b)) * min(a, b); } void write(long long int a[], long long int n) { for (int i = 0; i < n; i++) cout << a[i] << " "; cout << "\n"; } void show(vector<long long int> v) { for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << "\n"; } bool checkvalid(string expr) { stack<char> s; char x; for (int i = 1; i < expr.length() - 1; i++) { if (expr[i] == '(' || expr[i] == '[' || expr[i] == '{') { s.push(expr[i]); continue; } if (s.empty()) return false; switch (expr[i]) { case ')': x = s.top(); s.pop(); if (x == '{' || x == '[') return false; break; case '}': x = s.top(); s.pop(); if (x == '(' || x == '[') return false; break; case ']': x = s.top(); s.pop(); if (x == '(' || x == '{') return false; break; } } return (s.empty()); } void solve() { long long int n; cin >> n; string s; cin >> s; if (n % 2 == 1) cout << ":(" << "\n"; else { int x = n / 2; int o = 0, c = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') x--; else c++; } for (int i = 0; i < n; i++) { if (s[i] == '?') { x--; s[i] = '('; } if (x == 0) break; } for (int i = 0; i < n; i++) { if (s[i] == '?') { s[i] = ')'; } } if (x != 0) { cout << ":(" << "\n"; return; } else { if (checkvalid(s) and s[0] == '(' and s[n - 1] == ')') cout << s << "\n"; else { cout << ":(" << "\n"; } } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int t = 1; while (t--) solve(); }
### Prompt Generate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; bool isPrime(int num) { if (num == 0) return false; bool flag = true; for (int i = 2; i <= sqrt(num); i++) { if (num % i == 0) { flag = false; break; } } return flag; } long long int powermodm(long long int x, long long int n, long long int M) { long long int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } long long int power(long long int _a, long long int _b) { long long int _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a); _b /= 2; _a = (_a * _a); } return _r; } long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } long long int lcm(long long int a, long long int b) { return (max(a, b) / gcd(a, b)) * min(a, b); } void write(long long int a[], long long int n) { for (int i = 0; i < n; i++) cout << a[i] << " "; cout << "\n"; } void show(vector<long long int> v) { for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << "\n"; } bool checkvalid(string expr) { stack<char> s; char x; for (int i = 1; i < expr.length() - 1; i++) { if (expr[i] == '(' || expr[i] == '[' || expr[i] == '{') { s.push(expr[i]); continue; } if (s.empty()) return false; switch (expr[i]) { case ')': x = s.top(); s.pop(); if (x == '{' || x == '[') return false; break; case '}': x = s.top(); s.pop(); if (x == '(' || x == '[') return false; break; case ']': x = s.top(); s.pop(); if (x == '(' || x == '{') return false; break; } } return (s.empty()); } void solve() { long long int n; cin >> n; string s; cin >> s; if (n % 2 == 1) cout << ":(" << "\n"; else { int x = n / 2; int o = 0, c = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') x--; else c++; } for (int i = 0; i < n; i++) { if (s[i] == '?') { x--; s[i] = '('; } if (x == 0) break; } for (int i = 0; i < n; i++) { if (s[i] == '?') { s[i] = ')'; } } if (x != 0) { cout << ":(" << "\n"; return; } else { if (checkvalid(s) and s[0] == '(' and s[n - 1] == ')') cout << s << "\n"; else { cout << ":(" << "\n"; } } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int t = 1; while (t--) solve(); } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 300005; char str[MAXN]; int main() { int n; scanf("%d%s", &n, str); queue<int> q; int cnt = 0; for (int i = 0; i < n; i++) { if (str[i] == '(') cnt++; else if (str[i] == ')') cnt--; else str[i] = ')', cnt--, q.push(i); while (cnt < (i < n - 1)) { if (!q.empty()) { str[q.front()] = '('; q.pop(); cnt += 2; } else return 0 * printf(":(\n"); } } if (cnt == 0) printf("%s\n", str); else printf(":(\n"); return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 300005; char str[MAXN]; int main() { int n; scanf("%d%s", &n, str); queue<int> q; int cnt = 0; for (int i = 0; i < n; i++) { if (str[i] == '(') cnt++; else if (str[i] == ')') cnt--; else str[i] = ')', cnt--, q.push(i); while (cnt < (i < n - 1)) { if (!q.empty()) { str[q.front()] = '('; q.pop(); cnt += 2; } else return 0 * printf(":(\n"); } } if (cnt == 0) printf("%s\n", str); else printf(":(\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n; char s[N], ss[N]; int main() { scanf("%d", &n); scanf("%s", s + 1); int l = 0, r = 0; for (int i = 1; i <= n; i++) { ss[i] = s[i]; if (s[i] == '(') ++l; else if (s[i] == ')') ++r; else if (s[i] == '?') { ++l; ss[i] = '('; } } if ((l - r) % 2 == 1) { puts(":("); return 0; } if (r != l) { for (int i = n; i >= 1; i--) if (s[i] == '?') { ++r; l--; ss[i] = ')'; if (l == r) break; } } if (r != l) { puts(":("); return 0; } l = 0; for (int i = 1; i <= n; i++) { if (ss[i] == '(') ++l; else l--; if (i != n && l == 0) { puts(":("); return 0; } if (i == n && l != 0) { puts(":("); return 0; } if (l < 0) { puts(":("); return 0; } } for (int i = 1; i <= n; i++) printf("%c", ss[i]); return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n; char s[N], ss[N]; int main() { scanf("%d", &n); scanf("%s", s + 1); int l = 0, r = 0; for (int i = 1; i <= n; i++) { ss[i] = s[i]; if (s[i] == '(') ++l; else if (s[i] == ')') ++r; else if (s[i] == '?') { ++l; ss[i] = '('; } } if ((l - r) % 2 == 1) { puts(":("); return 0; } if (r != l) { for (int i = n; i >= 1; i--) if (s[i] == '?') { ++r; l--; ss[i] = ')'; if (l == r) break; } } if (r != l) { puts(":("); return 0; } l = 0; for (int i = 1; i <= n; i++) { if (ss[i] == '(') ++l; else l--; if (i != n && l == 0) { puts(":("); return 0; } if (i == n && l != 0) { puts(":("); return 0; } if (l < 0) { puts(":("); return 0; } } for (int i = 1; i <= n; i++) printf("%c", ss[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; bool fine = 1; fine &= (n % 2 != 1); int open = 0; for (auto it : s) { open += (it == '('); } open = n / 2 - open; for (auto& it : s) { if (it == '?' && open > 0) { it = '('; open--; } } for (auto& it : s) { if (it == '?') { it = ')'; } } int bal = 0; for (int i = 0; i < n; i++) { bal += (s[i] == '('); bal -= (s[i] == ')'); if (i < n - 1) { fine &= (bal > 0); } else { fine &= (bal == 0); } } fine &= (bal == 0); if (fine) { cout << s << endl; } else { cout << ":(" << endl; } }
### Prompt In CPP, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n >> s; bool fine = 1; fine &= (n % 2 != 1); int open = 0; for (auto it : s) { open += (it == '('); } open = n / 2 - open; for (auto& it : s) { if (it == '?' && open > 0) { it = '('; open--; } } for (auto& it : s) { if (it == '?') { it = ')'; } } int bal = 0; for (int i = 0; i < n; i++) { bal += (s[i] == '('); bal -= (s[i] == ')'); if (i < n - 1) { fine &= (bal > 0); } else { fine &= (bal == 0); } } fine &= (bal == 0); if (fine) { cout << s << endl; } else { cout << ":(" << endl; } } ```
#include <bits/stdc++.h> int main() { int l; std::cin >> l; std::string s; std::cin >> s; bool poss = true; int diff = 0; int open = 0; for (int i = 0; i < l; i++) open += (s[i] == '('); for (int i = 0; i < l; i++) { if (s[i] == '?') { if (open < l / 2) { s[i] = '('; open++; } else s[i] = ')'; } } for (int i = 0; i < l; i++) { if (diff <= 0 && i > 0) poss = false; if (s[i] == '(') diff++; else diff--; } if (poss && (diff == 0)) std::cout << s; else std::cout << ":("; return 0; }
### Prompt Generate a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> int main() { int l; std::cin >> l; std::string s; std::cin >> s; bool poss = true; int diff = 0; int open = 0; for (int i = 0; i < l; i++) open += (s[i] == '('); for (int i = 0; i < l; i++) { if (s[i] == '?') { if (open < l / 2) { s[i] = '('; open++; } else s[i] = ')'; } } for (int i = 0; i < l; i++) { if (diff <= 0 && i > 0) poss = false; if (s[i] == '(') diff++; else diff--; } if (poss && (diff == 0)) std::cout << s; else std::cout << ":("; return 0; } ```
#include <bits/stdc++.h> using namespace std; void err() { cout << ":(" << endl; exit(0); } int main() { int n; string s; cin >> n >> s; int cnt = n >> 1; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt--; } if (cnt < 0) err(); for (int i = 0; i < n; i++) { if (s[i] == '?') { if (cnt) { s[i] = '('; cnt--; } else { s[i] = ')'; } } } int dpt = 0; for (int i = 0; i <= n - 1; i++) { if (s[i] == '(') dpt++; if (s[i] == ')') dpt--; if (i < n - 1 && dpt <= 0) err(); } if (dpt) err(); cout << s << endl; return 0; }
### Prompt In cpp, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; void err() { cout << ":(" << endl; exit(0); } int main() { int n; string s; cin >> n >> s; int cnt = n >> 1; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt--; } if (cnt < 0) err(); for (int i = 0; i < n; i++) { if (s[i] == '?') { if (cnt) { s[i] = '('; cnt--; } else { s[i] = ')'; } } } int dpt = 0; for (int i = 0; i <= n - 1; i++) { if (s[i] == '(') dpt++; if (s[i] == ')') dpt--; if (i < n - 1 && dpt <= 0) err(); } if (dpt) err(); cout << s << endl; return 0; } ```
#include <bits/stdc++.h> char s[300000 + 5]; int n; int main() { scanf("%d", &n); scanf("%s", s + 1); if (n & 1) { puts(":("); return 0; } if (s[n] == '?') { s[n] = ')'; } if (s[1] == '?') { s[1] = '('; } if (s[1] == ')' || s[n] == '(') { puts(":("); return 0; } int sum = 0; int total = 0; for (int i = 2; i < n; i++) { if (s[i] == ')') { total++; } } for (int i = n - 1; i > 1; i--) { if (s[i] == '?') { if (total < ((n - 2) >> 1)) { total++; s[i] = ')'; sum++; } else { s[i] = '('; sum--; } } else { if (s[i] == ')') { sum++; } else { sum--; if (sum < 0) { puts(":("); return 0; } } } } sum = 0; for (int i = 2; i < n; i++) { if (s[i] == ')') { sum--; } else { sum++; } if (sum < 0) { puts(":("); return 0; } } puts(s + 1); return 0; }
### Prompt Create a solution in cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> char s[300000 + 5]; int n; int main() { scanf("%d", &n); scanf("%s", s + 1); if (n & 1) { puts(":("); return 0; } if (s[n] == '?') { s[n] = ')'; } if (s[1] == '?') { s[1] = '('; } if (s[1] == ')' || s[n] == '(') { puts(":("); return 0; } int sum = 0; int total = 0; for (int i = 2; i < n; i++) { if (s[i] == ')') { total++; } } for (int i = n - 1; i > 1; i--) { if (s[i] == '?') { if (total < ((n - 2) >> 1)) { total++; s[i] = ')'; sum++; } else { s[i] = '('; sum--; } } else { if (s[i] == ')') { sum++; } else { sum--; if (sum < 0) { puts(":("); return 0; } } } } sum = 0; for (int i = 2; i < n; i++) { if (s[i] == ')') { sum--; } else { sum++; } if (sum < 0) { puts(":("); return 0; } } puts(s + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string str; cin >> str; if (str.front() == ')' || str.back() == '(') { cout << ":(" << endl; return 0; } string s(n, ' '); int countP = 0; int countQ = 0; int countI = 0; for (int i = 0; i < n; i++) { if (str[i] == '(') countP++; else if (str[i] == ')') countQ++; else countI++; } if (countI >= abs(countP - countQ) && (countI - abs(countP - countQ)) % 2 == 0) { int num1 = (countI - abs(countP - countQ)) / 2, num2 = (countI - abs(countP - countQ)) / 2; if (countQ >= countP) { num1 += countQ - countP; } else num2 += countP - countQ; for (int i = 0; i < n; i++) { if (str[i] == '?') { if (num1 > 0) { s[i] = '('; num1--; } else { s[i] = ')'; num2--; } } else { s[i] = str[i]; } } int dept = 1; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') dept++; else if (s[i] == ')') dept--; if (dept == 0 || s.front() == ')' || s.back() == '(') { cout << ":(" << endl; return 0; } } cout << s << endl; } else cout << ":(" << endl; }
### Prompt Create a solution in Cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string str; cin >> str; if (str.front() == ')' || str.back() == '(') { cout << ":(" << endl; return 0; } string s(n, ' '); int countP = 0; int countQ = 0; int countI = 0; for (int i = 0; i < n; i++) { if (str[i] == '(') countP++; else if (str[i] == ')') countQ++; else countI++; } if (countI >= abs(countP - countQ) && (countI - abs(countP - countQ)) % 2 == 0) { int num1 = (countI - abs(countP - countQ)) / 2, num2 = (countI - abs(countP - countQ)) / 2; if (countQ >= countP) { num1 += countQ - countP; } else num2 += countP - countQ; for (int i = 0; i < n; i++) { if (str[i] == '?') { if (num1 > 0) { s[i] = '('; num1--; } else { s[i] = ')'; num2--; } } else { s[i] = str[i]; } } int dept = 1; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') dept++; else if (s[i] == ')') dept--; if (dept == 0 || s.front() == ')' || s.back() == '(') { cout << ":(" << endl; return 0; } } cout << s << endl; } else cout << ":(" << endl; } ```
#include <bits/stdc++.h> using namespace std; const int manx = 3e5 + 200; const long long mod = 1e9 + 7; char str[manx]; int main() { int n, L, R; scanf("%d%s", &n, str + 1); int flag = 1; L = R = 0; for (int i = 1; i <= n; i++) { if (str[i] == '(') L++; else if (str[i] == ')') R++; } int s1, s2; s1 = s2 = 0; if (n % 2 == 1) flag = 0; if (L > n / 2 || R > n / 2) flag = 0; L = n / 2 - L; R = n / 2 - R; if (str[1] == ')' || str[n] == '(') flag = 0; if (str[1] == '?') { str[1] = '('; L--; } if (str[n] == '?') { str[n] = ')'; R--; } if (str[1] == '(') s1++; if (L < 0 || R < 0) flag = 0; for (int i = 2; i < n && flag; i++) { if (str[i] == '(') s1++; else if (str[i] == ')') s2++; else { if (L) { str[i] = '('; s1++; L--; } else { str[i] = ')'; s2++; R--; } } if (L < 0 || R < 0 || s2 >= s1) flag = 0; } if (flag) printf("%s\n", str + 1); else printf(":(\n"); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int manx = 3e5 + 200; const long long mod = 1e9 + 7; char str[manx]; int main() { int n, L, R; scanf("%d%s", &n, str + 1); int flag = 1; L = R = 0; for (int i = 1; i <= n; i++) { if (str[i] == '(') L++; else if (str[i] == ')') R++; } int s1, s2; s1 = s2 = 0; if (n % 2 == 1) flag = 0; if (L > n / 2 || R > n / 2) flag = 0; L = n / 2 - L; R = n / 2 - R; if (str[1] == ')' || str[n] == '(') flag = 0; if (str[1] == '?') { str[1] = '('; L--; } if (str[n] == '?') { str[n] = ')'; R--; } if (str[1] == '(') s1++; if (L < 0 || R < 0) flag = 0; for (int i = 2; i < n && flag; i++) { if (str[i] == '(') s1++; else if (str[i] == ')') s2++; else { if (L) { str[i] = '('; s1++; L--; } else { str[i] = ')'; s2++; R--; } } if (L < 0 || R < 0 || s2 >= s1) flag = 0; } if (flag) printf("%s\n", str + 1); else printf(":(\n"); return 0; } ```
#include <bits/stdc++.h> namespace myland { using namespace std; namespace _abbr { const double EPS(1e-8); const double PI(acos(-1.0)); const int INF(0x3f3f3f3f); const long long INFL(0x3f3f3f3f3f3f3f3fll); const int MOD(1e9 + 7); } // namespace _abbr using namespace _abbr; namespace _solve {} using namespace _solve; namespace _calculate { bool odd(long long x) { return x & 1; } bool even(long long x) { return (x & 1) ^ 1; } bool posi(long long x) { return x > 0; } bool nega(long long x) { return x < 0; } bool zero(long long x) { return x == 0; } bool prime(long long x) { if (x < 2) return 0; for (int i = 2; i * i <= x; i++) if (x % i == 0) return 0; return 1; } long long droot(long long x) { return 1 + (x - 1) % 9; } long long upd(long long a, long long b) { return a % b ? a / b + 1 : a / b; }; long long random(long long a, long long b) { return a + rand() * rand() % (b - a + 1); }; long long bitn(long long x) { long long c = 0; while (x) c++, x >>= 1; return c; } template <class T> T sqr(T x) { return x * x; } long long qpow(long long a, long long n, long long mod = MOD) { long long res(1); while (n) { if (n & 1) (res *= a) %= mod; (a *= a) %= mod; n >>= 1; } return res % mod; } long long inv(long long a, long long mod = MOD) { return qpow(a, mod - 2); } template <class T> void tomin(T& a, T b) { if (b < a) a = b; } template <class T> void tomax(T& a, T b) { if (b > a) a = b; } } // namespace _calculate using namespace _calculate; namespace _simple_algo { long long stol(const string& s) { long long x = 0; for (char c : s) x = x * 10 + c - 48; return x; } string ltos(long long x) { string s = ""; if (x == 0) return "0"; while (x) s = char(x % 10 + 48) + s, x /= 10; return s; } bool pal(const string& s) { int l = s.size(); for (int i = 0, j = l - 1; i < j; i++, j--) if (s[i] != s[j]) return 0; return 1; } } // namespace _simple_algo using namespace _simple_algo; namespace _io { template <class T> void rd(T& x) { cin >> x; } long long rd() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void rd(int& x) { x = rd(); } void rd(long long& x) { x = rd(); } template <class A, class B> void rd(A& a, B& b) { rd(a), rd(b); } template <class A, class B> void rd(pair<A, B>& p) { cin >> p.first >> p.second; } template <class T> void wt(const T& x) { cout << x << endl; } template <class T> void wt(const T& x, char c) { cout << x << c; } template <class T> void wt(const T& x, const string& s) { cout << x << s; } template <class T> void wt(const T& x, int rnd) { cout << fixed << setprecision(rnd) << x << endl; } template <class T> void wt(const vector<T>& v) { for (T x : v) wt(x, ' '); wt(""); } template <class A, class B> void wt(const pair<A, B>& make_pair) { cout << make_pair.first << ' ' << make_pair.second << endl; } } // namespace _io using namespace _io; } // namespace myland using namespace myland; const int N(3e5 + 5); int n; char s[N]; bool ok() { int c1(0), c2(0), n1(0), n2(0); for (int i = 0; i <= n - 1; i++) c1 += s[i] == '(', c2 += s[i] == ')'; if (c1 > n / 2 || c2 > n / 2) return 0; for (int i = 0; i <= n - 1; i++) { if (s[i] == '?') { if (c1 >= n / 2) s[i] = ')', n1++; else s[i] = '(', n2++, c1++; } else { if (s[i] == '(') n2++; else n1++; } if (n1 >= n2 && i != n - 1) return 0; } return 1; } int main() { rd(n); scanf("%s", s); if (n & 1) wt(":("), exit(0); if (ok()) wt(s); else wt(":("), exit(0); }
### Prompt Your task is to create a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> namespace myland { using namespace std; namespace _abbr { const double EPS(1e-8); const double PI(acos(-1.0)); const int INF(0x3f3f3f3f); const long long INFL(0x3f3f3f3f3f3f3f3fll); const int MOD(1e9 + 7); } // namespace _abbr using namespace _abbr; namespace _solve {} using namespace _solve; namespace _calculate { bool odd(long long x) { return x & 1; } bool even(long long x) { return (x & 1) ^ 1; } bool posi(long long x) { return x > 0; } bool nega(long long x) { return x < 0; } bool zero(long long x) { return x == 0; } bool prime(long long x) { if (x < 2) return 0; for (int i = 2; i * i <= x; i++) if (x % i == 0) return 0; return 1; } long long droot(long long x) { return 1 + (x - 1) % 9; } long long upd(long long a, long long b) { return a % b ? a / b + 1 : a / b; }; long long random(long long a, long long b) { return a + rand() * rand() % (b - a + 1); }; long long bitn(long long x) { long long c = 0; while (x) c++, x >>= 1; return c; } template <class T> T sqr(T x) { return x * x; } long long qpow(long long a, long long n, long long mod = MOD) { long long res(1); while (n) { if (n & 1) (res *= a) %= mod; (a *= a) %= mod; n >>= 1; } return res % mod; } long long inv(long long a, long long mod = MOD) { return qpow(a, mod - 2); } template <class T> void tomin(T& a, T b) { if (b < a) a = b; } template <class T> void tomax(T& a, T b) { if (b > a) a = b; } } // namespace _calculate using namespace _calculate; namespace _simple_algo { long long stol(const string& s) { long long x = 0; for (char c : s) x = x * 10 + c - 48; return x; } string ltos(long long x) { string s = ""; if (x == 0) return "0"; while (x) s = char(x % 10 + 48) + s, x /= 10; return s; } bool pal(const string& s) { int l = s.size(); for (int i = 0, j = l - 1; i < j; i++, j--) if (s[i] != s[j]) return 0; return 1; } } // namespace _simple_algo using namespace _simple_algo; namespace _io { template <class T> void rd(T& x) { cin >> x; } long long rd() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void rd(int& x) { x = rd(); } void rd(long long& x) { x = rd(); } template <class A, class B> void rd(A& a, B& b) { rd(a), rd(b); } template <class A, class B> void rd(pair<A, B>& p) { cin >> p.first >> p.second; } template <class T> void wt(const T& x) { cout << x << endl; } template <class T> void wt(const T& x, char c) { cout << x << c; } template <class T> void wt(const T& x, const string& s) { cout << x << s; } template <class T> void wt(const T& x, int rnd) { cout << fixed << setprecision(rnd) << x << endl; } template <class T> void wt(const vector<T>& v) { for (T x : v) wt(x, ' '); wt(""); } template <class A, class B> void wt(const pair<A, B>& make_pair) { cout << make_pair.first << ' ' << make_pair.second << endl; } } // namespace _io using namespace _io; } // namespace myland using namespace myland; const int N(3e5 + 5); int n; char s[N]; bool ok() { int c1(0), c2(0), n1(0), n2(0); for (int i = 0; i <= n - 1; i++) c1 += s[i] == '(', c2 += s[i] == ')'; if (c1 > n / 2 || c2 > n / 2) return 0; for (int i = 0; i <= n - 1; i++) { if (s[i] == '?') { if (c1 >= n / 2) s[i] = ')', n1++; else s[i] = '(', n2++, c1++; } else { if (s[i] == '(') n2++; else n1++; } if (n1 >= n2 && i != n - 1) return 0; } return 1; } int main() { rd(n); scanf("%s", s); if (n & 1) wt(":("), exit(0); if (ok()) wt(s); else wt(":("), exit(0); } ```
#include <bits/stdc++.h> using namespace std; bool valid(const string& ss) { size_t top = 0; string s = ss; for (char c : s) switch (c) { case '(': s[top++] = c; break; case ')': if (!top || s[--top] != '(') return false; break; } return top == 0; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; std::string s; cin >> n >> s; if (n % 2 != 0 || s.front() == ')' || s.back() == '(') { cout << ":(" << endl; return 0; } int balance = 0; for (int i = 1; i < n - 1; ++i) { if (s[i] == '(') balance += 1; if (s[i] == ')') balance -= 1; } int l = 1; int r = n - 2; while (l <= r) { if (balance > 0) { if (s[r] == '?') { balance--; s[r] = ')'; } r--; } else { if (s[l] == '?') { s[l] = '('; balance++; } l++; } } s.front() = '('; s.back() = ')'; if (balance || !valid(s.substr(1, s.size() - 2))) { cout << ":(" << endl; return 0; } cout << s << endl; return 0; }
### Prompt Generate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool valid(const string& ss) { size_t top = 0; string s = ss; for (char c : s) switch (c) { case '(': s[top++] = c; break; case ')': if (!top || s[--top] != '(') return false; break; } return top == 0; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; std::string s; cin >> n >> s; if (n % 2 != 0 || s.front() == ')' || s.back() == '(') { cout << ":(" << endl; return 0; } int balance = 0; for (int i = 1; i < n - 1; ++i) { if (s[i] == '(') balance += 1; if (s[i] == ')') balance -= 1; } int l = 1; int r = n - 2; while (l <= r) { if (balance > 0) { if (s[r] == '?') { balance--; s[r] = ')'; } r--; } else { if (s[l] == '?') { s[l] = '('; balance++; } l++; } } s.front() = '('; s.back() = ')'; if (balance || !valid(s.substr(1, s.size() - 2))) { cout << ":(" << endl; return 0; } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; namespace _ { bool rEOF = 1; inline char nc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && rEOF && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? (rEOF = 0, EOF) : *p1++; } template <class _T> inline bool read(_T &num) { char c = nc(), f = 1; num = 0; while (c < '0' || c > '9') c == '-' && (f = -1), c = nc(); while (c >= '0' && c <= '9') num = num * 10 + c - '0', c = nc(); return (bool)(num *= f); } inline bool need(char &c) { return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'; } inline bool read_str(char *a) { while ((*a = nc()) && need(*a) && rEOF) ++a; *a = '\0'; return rEOF; } } // namespace _ using namespace _; char a[300005]; stack<int> s; int main() { int n; scanf("%d", &n); scanf("%s", a + 1); if (!((a[1] == '(' || a[1] == '?') && (a[n] == ')' || a[n] == '?'))) { printf(":(\n"); return 0; } if (n % 2 == 1) { printf(":(\n"); return 0; } a[1] = '(', a[n] = ')'; int k1 = (n - 2) / 2; int k2 = k1; for (int i = 2; i <= n - 1; i++) { if (a[i] == '(') k1--; if (a[i] == ')') k2--; } for (int i = 2; i <= n - 1; i++) { if (a[i] == '?') { if (k1 > 0) a[i] = '(', k1--; else if (k2 > 0) a[i] = ')', k2--; } } if (k1 < 0 || k2 < 0) { printf(":(\n"); return 0; } for (int i = 1; i <= n; i++) if (a[i] == '?') { printf(":(\n"); return 0; } for (int i = 1; i <= n; i++) { if (a[i] == '(') s.push(1); else s.pop(); if (s.size() == 0 && i != n) { printf(":(\n"); return 0; } } printf("%s\n", a + 1); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; namespace _ { bool rEOF = 1; inline char nc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && rEOF && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? (rEOF = 0, EOF) : *p1++; } template <class _T> inline bool read(_T &num) { char c = nc(), f = 1; num = 0; while (c < '0' || c > '9') c == '-' && (f = -1), c = nc(); while (c >= '0' && c <= '9') num = num * 10 + c - '0', c = nc(); return (bool)(num *= f); } inline bool need(char &c) { return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'; } inline bool read_str(char *a) { while ((*a = nc()) && need(*a) && rEOF) ++a; *a = '\0'; return rEOF; } } // namespace _ using namespace _; char a[300005]; stack<int> s; int main() { int n; scanf("%d", &n); scanf("%s", a + 1); if (!((a[1] == '(' || a[1] == '?') && (a[n] == ')' || a[n] == '?'))) { printf(":(\n"); return 0; } if (n % 2 == 1) { printf(":(\n"); return 0; } a[1] = '(', a[n] = ')'; int k1 = (n - 2) / 2; int k2 = k1; for (int i = 2; i <= n - 1; i++) { if (a[i] == '(') k1--; if (a[i] == ')') k2--; } for (int i = 2; i <= n - 1; i++) { if (a[i] == '?') { if (k1 > 0) a[i] = '(', k1--; else if (k2 > 0) a[i] = ')', k2--; } } if (k1 < 0 || k2 < 0) { printf(":(\n"); return 0; } for (int i = 1; i <= n; i++) if (a[i] == '?') { printf(":(\n"); return 0; } for (int i = 1; i <= n; i++) { if (a[i] == '(') s.push(1); else s.pop(); if (s.size() == 0 && i != n) { printf(":(\n"); return 0; } } printf("%s\n", a + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; void cutoff() { cout << ":("; exit(0); } int a = 0, b = 0; int main() { int n; string str; cin >> n >> str; if (n % 2 == 1) cutoff(); for (int x = 0; x < n; x++) { if (str[x] == '(') a++; if (str[x] == ')') b++; } a = n / 2 - a; b = n / 2 - b; if (a < 0 || b < 0) cutoff(); int sum = 0; for (int x = 0; x < n; x++) { if (str[x] == '(' || str[x] == ')') { sum += (str[x] == '(') ? 1 : -1; } else { if (a != 0) str[x] = '(', sum++, a--; else str[x] = ')', sum--, b--; } if (sum <= 0 && x != n - 1) { cutoff(); } } cout << str; return 0; }
### Prompt In cpp, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; void cutoff() { cout << ":("; exit(0); } int a = 0, b = 0; int main() { int n; string str; cin >> n >> str; if (n % 2 == 1) cutoff(); for (int x = 0; x < n; x++) { if (str[x] == '(') a++; if (str[x] == ')') b++; } a = n / 2 - a; b = n / 2 - b; if (a < 0 || b < 0) cutoff(); int sum = 0; for (int x = 0; x < n; x++) { if (str[x] == '(' || str[x] == ')') { sum += (str[x] == '(') ? 1 : -1; } else { if (a != 0) str[x] = '(', sum++, a--; else str[x] = ')', sum--, b--; } if (sum <= 0 && x != n - 1) { cutoff(); } } cout << str; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n % 2) { cout << ":(" << endl; return 0; } int cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt++; } for (int i = 0; i < n; i++) { if (s[i] == '?') { if (cnt < n / 2) { s[i] = '('; cnt++; } else s[i] = ')'; } } cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt++; else cnt--; if (cnt < 0 || (cnt == 0 && i != n - 1)) { cout << ":(" << endl; return 0; } } if (cnt) cout << ":(" << endl; else cout << s << endl; return 0; }
### Prompt Generate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n % 2) { cout << ":(" << endl; return 0; } int cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt++; } for (int i = 0; i < n; i++) { if (s[i] == '?') { if (cnt < n / 2) { s[i] = '('; cnt++; } else s[i] = ')'; } } cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt++; else cnt--; if (cnt < 0 || (cnt == 0 && i != n - 1)) { cout << ":(" << endl; return 0; } } if (cnt) cout << ":(" << endl; else cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int n; string s; int main() { cin >> n >> s, s = ' ' + s; int dem = 0; for (int i = 1; i <= n; i++) if (s[i] == '(') ++dem; int cnt = 0; for (int i = 1; i <= n; i++) { if (s[i] == '?') { if (2 * dem < n) s[i] = '(', ++dem; else s[i] = ')'; } if (s[i] == '(') ++cnt; if (2 * cnt < i || (i == n && 2 * cnt != i) || (i != n && 2 * cnt == i)) return cout << ":(", 0; } for (int i = 1; i <= n; i++) cout << s[i]; }
### Prompt Construct a Cpp code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int n; string s; int main() { cin >> n >> s, s = ' ' + s; int dem = 0; for (int i = 1; i <= n; i++) if (s[i] == '(') ++dem; int cnt = 0; for (int i = 1; i <= n; i++) { if (s[i] == '?') { if (2 * dem < n) s[i] = '(', ++dem; else s[i] = ')'; } if (s[i] == '(') ++cnt; if (2 * cnt < i || (i == n && 2 * cnt != i) || (i != n && 2 * cnt == i)) return cout << ":(", 0; } for (int i = 1; i <= n; i++) cout << s[i]; } ```
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n; string s; cin >> n >> s; int f = 0; if (n % 2) f = 1; cin >> n >> s; int ct1 = 0; int ct2 = 0; for (auto i : s) if (i == '(') ct1++; else if (i == ')') ct2++; int need = n / 2 - ct1; for (int i = 0; i < n; i++) { if (s[i] == '?') { if (need-- > 0) s[i] = '('; else s[i] = ')'; } } int ct = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') ct++; else ct--; if (ct < 0) f = 1; if (ct == 0 && i != n - 1) f = 1; } if (ct != 0) f = 1; if (f) cout << ":("; else cout << s; }
### Prompt Develop a solution in cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n; string s; cin >> n >> s; int f = 0; if (n % 2) f = 1; cin >> n >> s; int ct1 = 0; int ct2 = 0; for (auto i : s) if (i == '(') ct1++; else if (i == ')') ct2++; int need = n / 2 - ct1; for (int i = 0; i < n; i++) { if (s[i] == '?') { if (need-- > 0) s[i] = '('; else s[i] = ')'; } } int ct = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') ct++; else ct--; if (ct < 0) f = 1; if (ct == 0 && i != n - 1) f = 1; } if (ct != 0) f = 1; if (f) cout << ":("; else cout << s; } ```