题目链接:http://oj.ecustacm.cn/problem.php?id=1321
((xx|xxx)x|(x|xx))xx
6
这题目的描述有点不清楚,这里放一张图就懂题意了
思路:
从前向后遍历。遇到(之后,进入dfs,遇到)之后跳出dfs,遇到x,统计x的数字。遇到 | 就比较大小。
#include <iostream> #include <algorithm> #include <string> #include <string.h> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <math.h> #include <cstdio> #include <iomanip> #include <time.h> #define LL long long #define INF 0x3f3f3f3f #define ls nod<<1 #define rs (nod<<1)+1 const int maxn = 1e9 + 10 ; const LL mod = 20010905; std::string s; int i; int dfs() { int ans = 0; int cnt = 0; int len = s.length(); while (i < len) { if (s[i] == ‘(‘) { i++; cnt += dfs(); } else if (s[i] == ‘)‘) { i++; break; } else if (s[i] == ‘|‘) { i++; ans = std::max(ans,cnt); cnt = 0; } else { i++; cnt++; } } return std::max(ans,cnt);; } int main() { std::cin >> s; printf("%d\n",dfs()); return 0; }
原文:https://www.cnblogs.com/-Ackerman/p/12245141.html