POJ 2955 Brackets

Source: Internet
Author: User

Links: http://poj.org/problem?id=2955


Brackets
Time limit:1000ms
Memory limit:65536k
Total submissions:4378
accepted:2331
Description

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, then AB 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, ai 1 ai 2 ... aim is a regular brackets sequence.

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


Input

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.


Output

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
66406

Source

Stanford Local 2004

To give you a sequence of parentheses of not more than 100 length, the length of the longest legal brace subsequence. The valid sequence is determined as: 1. The empty sequence is legal; 2. If a sequence s is legal, then (s) and [s] are both legal and 3. If two sequences A and B are legal, then AB is also legal. For example: (), [], (()), () [], () [()] are legal, and (,],) (, [)], ([[] neither is legal.


Idea--Typical interval DP model. Analysis of the problem can be found: if you find a pair of matching parentheses [xxx]oooo, the interval is divided into two parts, part xxx, the other part is oooo. Set Dp[i][j] represents the longest legal bracket subsequence length between interval [I,j], then dp[i][j]=dp[i+1][j] when i<j, if there is no bracket matching the i in the interval [i+1,j]; If there is a matching K, then dp[i][j] = max{dp[i+1][j],dp[i+1][k-1]+dp[k+1][j]+
2 (I+1<k<j&&i and k are pairs of matching parentheses)}. Therefore, we enumerate I from the end of the string to the end of the string, the last dp[0][len-1] is the answer, Len represents the string length.


Complexity analysis--time complexity: O (len^3), spatial complexity: O (len^2)


Attach the AC code:


#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip > #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include < algorithm> #include <queue> #include <vector> #include <set> #include <map>//#pragma comment (Linker, "/stack:102400000, 102400000") using namespace std;typedef unsigned int li;typedef long long ll;typedef unsigned l Ong long Ull;typedef long double ld;const double pi = ACOs ( -1.0); const DOUBLE E = exp (1.0); const double EPS = 1e-8;const i NT MaxLen = 105;char str[maxlen];int dp[maxlen][maxlen];bool match (char A, char b); Whether the parentheses match the int main () {Ios::sync_with_stdio (false), while (scanf ("%s", str) && strcmp ("End", str)) {memset (DP, 0, sizeof (DP)); initialize int len = strlen (str), for (int i=len-1; i>=0; i--) {//From behind widening interval for (int j=i+1; j<len; J + +) {//DP[I][J] = interval [i , j] the longest legal bracket subsequence length dp[i][j] = dp[i+1][j]; The interval [i+1,j] does not match the I bracket for (int k=i+1; k&Lt;=j; k++)//Find to match, split in Split (Match (Str[i], str[k])//compare size, take large dp[i][j] = max (dp[i][j], dp[i+1][k-1]+dp[k+1][j]+2);}} printf ("%d\n", Dp[0][len-1]);} return 0;} BOOL Match (char A, char b) {if ((a== ' (' &&b== ') ') | | (a== ' [' &&b== ']) return 1;return 0;}


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

POJ 2955 Brackets

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.