题解:
我们知道从左边往右边扫的时候如果左括号+?小于)那么一定会有)失衡无法匹配,右边扫过来同理。
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N=5010;
//LL p,k,cnt=0,ans[100000];
int ok[N][N];
void solve()
{
string s; cin>>s;
for(int i=0;i<s.length();i++){
int l=0,r=0,_=0;
for(int j=i;j<=s.length();j++){
if(s[j]=='(') l++;
else if(s[j]==')') r++;
else _++;
if(l+_<r) break;
ok[i][j]=1;
}
}
int res=0;
for(int i=s.length()-1;i>=0;i--){
int l=0,r=0,_=0;
for(int j=i;j>=0;j--){
if(s[j]=='(') l++;
else if(s[j]==')') r++;
else _++;
if(r+_<l) break;
if(ok[j][i]&&(i-j+1)%2==0) res++;
}
}
printf("%d\n",res);
}
signed main(){
solve();
#ifndef ONLINE_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}