Codeforces-1153C

C. Serval and Parenthesis Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.ios

In his favorite math class, the teacher taught him the following interesting definitions.算法

parenthesis sequence is a string, containing only characters "(" and ")".express

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.ide

We define that |s||s| as the length of string ss. A strict prefix s[1l]s[1…l] (1l<|s|)(1≤l<|s|) of a string s=s1s2s|s|s=s1s2…s|s| is string s1s2sls1s2…sl. 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 ss containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in ss 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.this

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.atom

Input

The first line contains a single integer |s||s| (1|s|31051≤|s|≤3⋅105), the length of the string.spa

The second line contains a string ss, containing only "(", ")" and "?".rest

Output

A single line contains a string representing the answer.code

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 ":(".

題目大意:給定一個只含有'(', ')', '?'三種字符的字符串,其中'?'能夠轉換成'(' 或 ')',要求判斷這個字符串是否符合除首尾任何'('的前面都沒有')'且整個字符串爲合法序列。

題解:這裏學習了xyq學長的假設算法

#include<iostream> #include<string>
using namespace std; const int N = 300005; int n, pos[N];//pos後綴意味着逆序時這個點有多少個')'
string s; int main() { ios::sync_with_stdio(false); cin >> n; cin >> s; if (n % 2) { cout << ":(" << endl; return 0; } if (s[0] == ')' || s[n - 1] == '(') { cout << ":(" << endl; return 0; } if (s[0] == '?') { s[0] = '('; } if (s[n - 1] == '?') { s[n - 1] = ')'; } for (int i = n - 2; i >= 1; i--) { pos[i] = pos[i + 1]; if (s[i] == ')' || s[i] == '?')//開局假設全部的 '?' 都是都是')'
            pos[i]++; else pos[i]--; } int lsum = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') lsum++; else if (s[i] == ')') { if (lsum == 0) { cout << ":(" << endl; return 0; } else lsum--; } else { if (lsum) { if (pos[i] > lsum) { s[i] = '('; lsum++; } else { s[i] = ')'; lsum--; } } else { s[i] = '('; lsum++; } } } if (lsum) { cout << ":(" << endl; } else cout << s << endl; return 0; }
View Code
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息