[CF]Codeforces Round #551 (Div. 2)

本文介绍了一种算法,用于解决括号序列问题,确保所有严格前缀不构成正确的括号序列,而整个序列则为正确括号序列。通过使用贪心策略替换未知字符,确保了最终序列的正确性。

C. Serval and Parenthesis Sequence

Description

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.

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

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 ?[1?]s[1…l] (1?<|?|)(1≤l<|s|) of a string ?=?1?2?|?|s=s1s2…s|s| is string ?1?2??s1s2…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 ?scontaining 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|?|31051≤|s|≤3⋅105), 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

 

正确解法:

首先n肯定是偶数
如果有 “(”,n1++  “)”,n2++.
如果 n1>n/2 || n2 > n/2  的时候,肯定也不行
然后就用贪心的操作,先用 (,没了再用 )
最后检查一遍就好啦!
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <map>
 8 #include <vector>
 9 using namespace std;
10 typedef long long ll;
11 const int inf=0x7fffffff;
12 const int N=100000+100;
13 char s[3*N];
14 int n,n1,n2,x;
15 void fc()
16 {
17     cout<<":("<<endl;
18     exit(0);
19 }
20 int main()
21 {
22     scanf("%d",&n);
23     scanf("%s",&s);
24     if(n%2==1)
25         fc();
26     for(int i=0;i<n;i++)
27     {
28         if(s[i]=='(')
29             n1++;
30         else if(s[i]==')')
31             n2++;
32     }
33     if(n1>n/2||n2>n/2)
34         fc();
35     n1=n/2-n1;
36     for(int i=0;i<n;i++)
37     {
38         if(s[i]=='?')
39         {
40             if(n1)
41             {
42                 s[i]='(';
43                 n1--;
44             }
45             else    s[i]=')';
46         }
47     }
48     for(int i=0;i+1<n;i++)
49     {
50         if(s[i]=='(')   x++;
51         else    x--;
52         if(x<=0)    fc();
53     }
54     printf("%s",s);
55 
56     return 0;
57 }
View Code

 

 

 

转载于:https://www.cnblogs.com/Kaike/p/10737130.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值