1. 程式人生 > >Codeforces-5C. Longest Regular Bracket Sequence

Codeforces-5C. Longest Regular Bracket Sequence

namespace %d blank main spa pos str href def

傳送門

#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; }

Codeforces-5C. Longest Regular Bracket Sequence