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;
    }
  • 相关阅读:
    浏览器缓存机制
    linux mail命令用法
    linux下Memcached安装以及PHP的调用
    JAVA和C# 3DES加密解密
    C++中函数调用时的三种参数传递方式详解

    const的用法,特别是用在函数前面与后面的区别!
    海底捞的“七宗罪”
    解决Qt5.7.0 cannot find -lGL
    怎么删除桌面右键"打开好桌道壁纸"
  • 原文地址:https://www.cnblogs.com/ACMessi/p/4842207.html
Copyright © 2011-2022 走看看