一道水题,只需要递归即可 AC。
因为连续括号嵌套贡献为
0
0
0,所以我们只需要看 ->
就行了。当 s[i]=='-'
时,答案就是
max
\max
max (->
左边的字符串的贡献+1,->
右边字符串的贡献)。
my code:
#include <bits/stdc++.h>
using namespace std;
string s;
int dg(int l,int r){
if(l+1==r)return 0;
int cur=0;
for(int i=l;i<=r;i++){
if(s[i]=='(') cur++;
else if(s[i]==')') cur--;
if(cur==0&&s[i]=='-')
return max(dg(l,i-1)+1,dg(i+2,r));
}
return dg(l+1,r-1);
}
signed main(){
cin>>s;
cout<<dg(0,s.size()-1)<<endl;
return 0;
}