51nod 1791 Legal Bracket Sub-segment (DP)

Source: Internet
Author: User
Tags valid

There is a sequence of parentheses, and now it is time to calculate how many of the non-empty segments it has is a valid bracket sequence.

The legal bracket sequence is defined as: An empty sequence is a valid bracket sequence. If S is a valid bracket sequence, then (s) is the legal bracket sequence. If both a and B are valid bracket sequences, then AB is the legal bracket sequence.


Multiple sets of test data.

The first line has an integer T (1<=t<=1100000) that represents the number of test data.

Next T, each line has a sequence of parentheses, which is a non-empty string consisting of ' (' and ') '.

The total length of all input bracket sequences does not exceed 1100000.


Output T line, each line corresponds to the answer to a test data.

Input Example

5 (() () () () (() (() (

Output Example



The position of the closing parenthesis corresponding to each opening parenthesis is preprocessed first, and Cnt[i]=j Cnt[i]=j represents the position of the closing parenthesis for the opening parenthesis I I, which is J J.

Dp[i] Dp[i] represents the number of valid bracket sequences on the right side of I I.

For each opening parenthesis, dp[i]=dp[cnt[i]+1]+1 dp[i]=dp[cnt[i]+1]+1, which indicates the number of valid parenthesis sequences on the right side of each opening parenthesis equals the left parenthesis (if any) adjacent to the right parenthesis of the current match, plus the number of the current pair.

AC Code

 #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f const int MAXN = 12000
const int mod = 1E9+7;
typedef long Long LL;
Stack<int> SK;
int CNT[MAXN];

    void Solve () {while (!sk.empty ()) Sk.pop ();
    int len = strlen (str);
        for (int i=0; i<=len+1; i++) {ans[i] = 0;
    Cnt[i] =-1;
        } for (int i=len-1; i>=0; i--) {if (str[i]== ') ') Sk.push (i);
            else {if (Sk.empty ()) continue;
            Cnt[i] = Sk.top ();
        Sk.pop ();
    }} LL res = 0;
            for (int i=len-1; i>=0; i--) {if (cnt[i]!=-1) {ans[i] = ans[cnt[i]+1]+1;
        Res + = Ans[i];
}} printf ("%lld\n", res);
    } int main () {int T;
    scanf ("%d%*c", &t);
        while (t--) {scanf ("%s", str);
    Solve (); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.