思路: DP[i] 表示以i结尾的方案数,模拟一遍括号匹配过程就好了
代码:
#include<bits/stdc++.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof(a))
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define bug puts("bug");
typedef long long ll;
typedef pair<ll,ll> pii;
const double PI=acos(-1);
const int maxn=1e6+10;
string s;
ll dp[maxn],t;
int main(){
cin>>t;
while(t--){
cin>>s;
stack<int> S;
dp[0]=0;
for(int i=0;i<s.size();i++){
if(s[i]=='('){
S.push(i+1);
}
else{
if(S.empty()) continue;
int f=S.top();
S.pop();
dp[i+1]+=dp[f-1]+1;
}
}
ll sum=0;
for(int i=0;i<=s.size();i++) sum+=dp[i],dp[i]=0;
cout<<sum<<endl;
}
return 0;
}