• Scaena Felix


    Scaena Felix

     Time Limit: 2000/1000 MS (Java/Others)
     
     Memory Limit: 65536/65536 K (Java/Others)
    Problem Description

    Given a parentheses sequence consist of '(' and ')', a modify can filp a parentheses, changing '(' to ')' or ')' to '('.

    If we want every not empty substring of this parentheses sequence not to be "paren-matching", how many times at least to modify this parentheses sequence?

    For example, "()","(())","()()" are "paren-matching" strings, but "((", ")(", "((()" are not.

    Input

    The first line of the input is a integer TT, meaning that there are TT test cases.

    Every test cases contains a parentheses sequence SS only consists of '(' and ')'.

    1 leq |S| leq 1,0001S1,000.

    Output

    For every test case output the least number of modification.

    Sample Input
    3
    ()
    ((((
    (())
    Sample Output
    1
    0
    2
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    char ch[1005];
    int sum[1005];
    int sum2[1005];
    int main(){
        int n;
        scanf("%d",&n);
        while(n--){
            memset(sum,0,sizeof(sum));
            memset(sum2,0,sizeof(sum2));
            scanf("%s",ch);
            int cnt = 0,cnt1 = 0,cnt2 = 0;
            int ans = 1e9;
            int len = strlen(ch);
            for(int i = 0; i < len; i++){
                if(ch[i] == '(')
                    sum[i+1] = sum[i] + 1;
                else sum[i+1] = sum[i];
            }
            for(int i = len-1; i > 0; i--){
                if(ch[i] == ')')
                    sum2[i-1] = sum2[i] + 1;
                else sum2[i-1] = sum2[i];
            }
            for(int i = 0; i < len; i++){
                ans = min(ans,sum[i] + sum2[i]);
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    如何一次插入多条记录的SQL语句
    oracle 创建联合主键语句
    02-36 支持向量回归
    245 第三篇:Django-路由控制
    244 第二篇:Django简介
    243 第一篇:自定义Web框架
    242 第一篇:Http协议详细介绍
    02-34 非线性支持向量机(鸢尾花分类)+自定义数据分类
    241 第一篇:web应用
    240 vue学习【第6篇】:vue之导入Bootstrap
  • 原文地址:https://www.cnblogs.com/ACMessi/p/4842207.html
Copyright © 2020-2023  润新知