POJ 2955 Brackets (interval dp bracket matching)

Source: Internet
Author: User

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3951 Accepted: 2078


We give the following inductive definition of a "regular brackets" sequence:

    • The empty sequence is a regular brackets sequence,
    • If s is a regular brackets sequence, then (s) and [s] is regular brackets sequences, and
    • If a and b are regular brackets sequences, thenAB is a regular brackets sequence.
    • No other sequence is a regular brackets sequence

For instance, all of the following character sequences is regular brackets sequences:

(), [], (()), ()[], ()[()]

While the following character sequences is not:

(, ], )(, ([)], ([(]

Given a brackets sequence of characters a1a2 ... An, your goal are to find the length of the longest regular brackets sequence, which is a subsequence ofs. That's, you wish to find the largest m such this for indicesi1, i2, ..., im where 1 ≤i1 < i2 < ... < imn, ai1ai 2 ... aim is a regular brackets sequence.

Given the initial sequence ([([]])] , the longest regular brackets subsequence is [([])] .


The input test file would contain multiple test cases. Each input test case consists of a single line containing only the characters ( , ) , [ , and ] ; St'll has length between 1 and inclusive. The End-of-file is marked by a line containing the word "end" and should not being processed.


For each input case, the program should print the length of the longest possible regular brackets subsequence on a single Line.

Sample Input

((())) () () () () ([][][)

Sample Output



Stanford Local 2004

Title Link: http://poj.org/problem?id=2955

Title: Give a sequence of parentheses, ask the number of valid parentheses in the sequence, if a is legal, then [a], (a) are legal, if A B is legal, AB is also legal

Title Analysis: and POJ 1141 that classic bracket matching similar, the problem is more simple, to find a way to transform the problem, since the maximum number of brackets required to match, we consider adding a minimum of parentheses, so that the entire sequence is legal, so it turned into 1141 that problem, The nature of the maximal matching of the brain-motion analogy binary graph, the maximum match + the maximum independent set = the number of points, obviously to add the least points to make the sequence legal, the least number of points is the | maximum independent set |, we require the original sequence of | maximum Match |, the above is purely yy, the following gives the transfer equation, and 11,411
Dp[i][i] = 1;
Then enumerate the interval lengths
1) Peripheral matching: dp[i][j] = dp[i + 1][j-1];
2) perimeter mismatch, enumeration split point: Dp[i][j] = min (Dp[i][j], dp[i][k] + dp[k + 1][j]); (I <= K < j)

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const INF = 0x3fffffff  ; Char s[205];int dp[205][205];int main () {while    (scanf ("%s", s)! = EOF && strcmp (S, "end")! = 0)    {        int Len = strlen (s);        memset (DP, 0, sizeof (DP));        for (int i = 0; i < len; i++)            dp[i][i] = 1;        for (int l = 1, l < len; l++)        {for            (int i = 0; i < len-l; i++)            {                Int j = i + L;                DP[I][J] = INF;                if ((s[i] = = ' (' && s[j] = = ') ') | | (S[i] = = ' [' && s[j] = = '] '))                    DP[I][J] = dp[i + 1][j-1];                for (int k = i; k < J; k++)                    dp[i][j] = min (Dp[i][j], dp[i][k] + dp[k + 1][j]);            }        }        printf ("%d\n", Len-dp[0][len-1]);}    }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2955 Brackets (interval dp bracket matching)

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.