Let us define a regular brackets sequence in the following way:
1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (S) is a regular sequence.
3. If A and B are regular sequences, then AB is a regular sequence.
For example, these sequences of characters are regular brackets sequences: (), (()), ()(), ()(()), ((())())().
And all the following character sequences are not: (, ), ((), ()), ())(, (()(, ()))().
A sequence of characters '(' and ')' is given. You can insert only one '(' or ')' into the left of the sequence, the right of the sequence, or the place between any two adjacent characters, to try changing this sequence to a regular brackets sequence.
InputThe first line has a integer T (1 <= T <= 200), means there are T test cases in total.
For each test case, there is a sequence of characters '(' and ')' in one line. The length of the sequence is in range [1, 105].
OutputFor each test case, print how many places there are, into which you insert a '(' or ')', can change the sequence to a regular brackets sequence.
What's more, you can assume there has at least one such place.
Sample Input4 ) ()) (()(()) ((())())(()Sample Output
1 3 7 3
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#define LL long long
using namespace std;
const int MAX = 1e6+10;
const int INF = 0x3fffffff;
int a[MAX];
char s[MAX];
int main(){
int t;
cin>>t;
getchar();
while(t--){
memset(a,0,sizeof(a));
memset(s,0,sizeof(s));
gets(s);
int ln = 0,rn = 0;
int len = strlen(s);
for(int i=0;i<len;i++){
if(s[i]=='('){
a[i] = -1;
ln++;
}
else{
rn++;
a[i] = 1;
}
}
if(rn<ln){//缺右括号
int sum = 0;
int temp =0;
for(int i=0;i<len;i++){
sum+=a[i];
if(sum<0)
temp++;
else if (sum==0)
temp = 0;
}
cout<<temp<<endl;
}
else{
int sum =0;
int temp = 0;
for(int i=len-1;i>=0;i--){
sum+=a[i];
if(sum > 0)
temp++;
else if(sum == 0)
temp=0;
}
cout<<temp<<endl;
}
}
return 0;
}