P8650 [蓝桥杯 2017 省 A] 正则问题
递归。令 D() 返回接下来的一个子表达式的答案。
考虑下一个字符:
x:答案接上x,答案加一。(:答案接上下一个子表达式,答案加D()。):该子表达式结束,返回答案。|:答案与下一个子表达式取 $\max$ 后该子表达式结束,返回答案与D()的最大值。
#include <cstdio>
#include <algorithm>
using namespace std;
int D()
{
char c;
int q = 0;
while (~(c = getchar()))
switch (c)
{
case 'x':
++q;
break;
case '(':
q += D();
break;
case ')':
return q;
case '|':
return max(q, D());
}
return q;
}
int main()
{
return !printf("%d", D());
}

浙公网安备 33010602011771号