二叉树跟递归搜索树,当遇到|时,字符串长度为左右两边取最大值,遇到积答案就加一,关键点:遇到左括号时必然会遇到右括号
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 110;
int k;
string s;
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() << endl;
return 0;
}