zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    IOS debug网络PonyDebugger 实践篇
    基于S7-200的PLC对里程轮(增量式码盘)解码的应用
    SICP 习题 (1.14)解题总结
    TQ210裸机编程(2)——LED流水灯
    DP练习(初级):ZigZag
    java 十六进制数的转换
    Oracle DB 执行表空间时间点恢复
    html中文乱码(解决办法)
    html的列表
    MySQL的模糊搜索
  • 原文地址:https://www.cnblogs.com/ACMessi/p/4842207.html
Copyright © 2011-2022 走看看