Codeforces-5C. Longest Regular Bracket Sequence
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f using namespace std; typedef long long LL; const int maxn = 1e6 + 10; char str[maxn]; int st[maxn]; int dp[maxn]; int main() { scanf("%s", str); int len = strlen(str); int ans = 0, cnt = 0, pos = 0; for (int i = 0; i < len; i++) { if (str[i] == '(') { st[pos++] = i; } else { if (pos) { int t = st[--pos]; dp[i] = dp[t - 1] + i - t + 1; if (dp[i] > ans) { ans = dp[i]; cnt = 1; } else if (dp[i] == ans) { cnt++; } } } } if (!ans) cnt = 1; printf("%d %d\n", ans, cnt); return 0; }