Codeforces-5C. Longest Regular Bracket Sequence

Posted xFANx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于Codeforces-5C. Longest Regular Bracket Sequence的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces-5C. Longest Regular Bracket Sequence

CodeForces 5C(DP初步_G题)解题报告

[leetcode-14-Longest Common Prefix]

Longest Palindromic Substring & Longest Palindromic Subsequence

[Lintcode]124. Longest Consecutive Sequence/[Leetcode]128. Longest Consecutive Sequence

Longest Palindromic Substring