地址

https://codeforces.com/contest/1153/problem/C

原文地址

https://www.lucien.ink/archives/415

代码

https://pasteme.cn/6250

#include <bits/stdc++.h>
const int maxn = int(1e6) + 7;
int len, left, right;
char str[maxn];
bool check() {
    std::stack<char> stack;
    if (len % 2 != 0) return false;
    left = right = len / 2;
    for (int i = 1; i <= len; i++) {
        if (str[i] == '(') left--;
    }
    for (int i = 1; i <= len; i++) {
        if (str[i] == '?') {
            if (left > 0) {
                str[i] = '(';
                left--;
            } else {
                str[i] = ')';
                right--;
            }
        } else {
            if (str[i] == ')') right--;
        }
    }
    if (left != 0 || right != 0) return false;
    for (int i = 1; i <= len; i++) {
        if (str[i] == '(') stack.push('(');
        else {
            if (stack.empty()) return false;
            if (stack.top() == '(') stack.pop();
        }
        if (i != len && stack.empty()) return false;
    }
    return stack.empty();
}
int main() {
    scanf("%d%s", &len, str + 1);
    if (check()) puts(str + 1);
    else puts(":(");
    return 0;
}
最后修改:2019 年 04 月 15 日
谢谢老板!