Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated, T-shirts + prizes!)

题目链接:https://codeforces.com/contest/1305

A - Kuroni and the Gifts

诚心诚意的签到题。

B - Kuroni and Simple Strings

题意:给一个括号串s。定义一个括号串是“简单括号串”,当且仅当其是"(((..("+")))...)"的形式,且前后两部分的长度相等。每次操作可以从括号串s中选择一个子序列,要求这个子序列是一个简单括号串,然后把整个子序列从s中删除。要求使用尽可能少的操作使得无法再操作。

题解:假如某个'('的右边还有')',那么显然还可以继续操作。假如每次只能消除一对'('和')',那么消除最左侧的'('和最右侧的')'是最好的,根据这个思路可以依次把最左和最右的括号匹配掉,当两边指针相遇之后就不会再有匹配的括号了。

char s[1005];
int ans[1005], atop;
 
void test_case() {
    scanf("%s", s + 1);
    int n = strlen(s + 1);
    int firstleft = -1, firstright = -1;
    for(int i = 1; i <= n; ++i) {
        if(s[i] == '(') {
            firstleft = i;
            break;
        }
    }
    if(firstleft == -1) {
        puts("0");
        return;
    }
    for(int i = firstleft + 1; i <= n; ++i) {
        if(s[i] == ')') {
            firstright = i;
            break;
        }
    }
    if(firstright == -1) {
        puts("0");
        return;
    }
    puts("1");
    int L = 1, R = n;
    atop = 0;
    while(L <= R) {
        if(s[L] == ')') {
            ++L;
            continue;
        }
        if(s[R] == '(') {
            --R;
            continue;
        }
        assert(L < R && s[L] == '(' && s[R] == ')');
        ans[++atop] = L;
        ans[++atop] = R;
        ++L;
        --R;
    }
    sort(ans + 1, ans + 1 + atop);
    printf("%d\n", atop);
    for(int i = 1; i <= atop; ++i)
        printf("%d%c", ans[i], " \n"[i == atop]);
}

猜你喜欢

转载自www.cnblogs.com/KisekiPurin2019/p/12406247.html