CF1153C Serval and Parenthesis Sequence
Posted wangyiming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1153C Serval and Parenthesis Sequence相关的知识,希望对你有一定的参考价值。
思路:
贪心,参考了https://codeforces.com/blog/entry/66539。
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int n; string s; 6 while (cin >> n >> s) 7 { 8 if (n & 1) { cout << ":(" << endl; continue; } 9 int l = 0; 10 for (int i = 0; i < n; i++) 11 { 12 if (s[i] == ‘(‘) l++; 13 } 14 if (l > n / 2) { cout << ":(" << endl; continue; } 15 for (int i = 0; i < n; i++) 16 { 17 if (l == n / 2) break; 18 else if (s[i] == ‘?‘) { s[i] = ‘(‘; l++; } 19 } 20 for (int i = 0; i < n; i++) 21 { 22 if (s[i] != ‘(‘) s[i] = ‘)‘; 23 } 24 l = 0; bool flg = true; 25 for (int i = 0; i < n; i++) 26 { 27 if (s[i] == ‘(‘) l++; 28 else 29 { 30 l--; 31 if (l < 0 || (l == 0 && i != n - 1)) { flg = false; break; } 32 } 33 } 34 if (l) flg = false; 35 cout << (flg ? s : ":(") << endl; 36 } 37 return 0; 38 }
以上是关于CF1153C Serval and Parenthesis Sequence的主要内容,如果未能解决你的问题,请参考以下文章
CF1153F Serval and Bonus Problem
CF1153C. Serval and Parenthesis Sequence
CF1153F Serval and Bonus Problem [积分,期望]
CF1153D Serval and Rooted Tree