#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define ll long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
char s[1100005];
stack<char>st;
int main(){
int t;
ll cnt;
ll ans;
scanf("%d",&t);
while(t--){
ll ans=0;
while(st.size()){
st.pop();
}
scanf(" %s",s);
int l=strlen(s);
for(int i=0;i<l;i++){
if(s[i]=='('){
st.push(s[i]);
}
else if(s[i]==')'){
if(st.size()==0){
st.push(s[i]);
}
else if(st.top()=='('){
st.pop();
st.push('|');
}
else if(st.top()=='|'){
cnt=0;
while(st.size()){
if(st.top()=='|'){
cnt++;
st.pop();
}
else break;
}
ans+=(cnt+1)*cnt/2;
if(st.size()){
if(st.top()=='('){
st.pop();
st.push('|');
}
else st.push(s[i]);
}
else st.push(s[i]);
}
else st.push(s[i]);
}
}
cnt=0;
while(st.size()){
if(st.top()=='|'){
while(st.size()){
if(st.top()=='|'){
cnt++;
st.pop();
}
else break;
}
ans+=(cnt+1)*cnt/2;
}
else {
cnt=0;
st.pop();
}
}
printf("%lld\n",ans);
}
return 0;
}
51nod1791 合法括号子段
最新推荐文章于 2022-01-20 16:46:34 发布