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\n",ans);
    }
    return 0;
}

 

posted @ 2015-09-27 14:32  Tobu  阅读(329)  评论(0编辑  收藏  举报