解题思路:类似遍历的搜索,如果当前遇到的( 就把括号里的x给求出,如果遇到 | 就比较 | 左右两边x的数量的大小,如果遇到 )那么就跳出,其他都是x直接累加
#include<bits/stdc++.h>
#define x first
#define y second
#define mem(h) memset(h,-1,sizeof h)
#define mcp(a,b) memcpy(a,b,sizeof b)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
namespace IO{
inline LL read(){
LL o=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){o=o*10+c-'0';c=getchar();}
return o*f;
}
}using namespace IO;
const int N=1e2+7,M=2e5+7,INF=0x3f3f3f3f,mod=1e8+7,P=131;
string s;
int k;
int dfs(){
int res=0;
while(k<s.size()){
if(s[k]=='('){
k++;
res+=dfs();
k++;
}else if(s[k]=='|'){
k++;
res=max(res,dfs());
}else if(s[k]==')'){
break;
}else{
k++;
res++;
}
}
return res;
}
int main(){
cin>>s;
cout<<dfs();
return 0;
}