Poj 2955 brackets (motion gauge)

Source: Internet
Author: User
Brackets
Time limit:1000 ms   Memory limit:65536 K
Total submissions:2999   Accepted:1536

Description

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

  • The empty sequence is a regular brackets sequence,
  • IfSIs a regular brackets sequence, then (S) And [S] Are regular brackets sequences, and
  • IfAAndBAre regular brackets sequences, thenABIs a regular brackets sequence.
  • No other sequence is a regular brackets Sequence

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

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

While the following character sequences are not:

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

Given a brackets sequence of CharactersA1A2...An, Your goal is to find the length of the longest regular brackets sequence that is a subsequenceS. That is, you wish to find the largestMSuch that for indicesI1,I2 ,...,ImWhere 1 ≤I1 <I2 <... <ImN,AI1AI2...AimIs a regular brackets sequence.

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

Input

The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters(,),[, And]; Each input test will have length between 1 and 100, intrusive. The end-of-file is marked by a line containing the word "end" and shocould not be processed.

Output

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

Sample Input

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

Sample output

66406

Source

Stanford local 2004
? If a pair of matching brackets [XXX] Oooo is found, the interval is divided into two parts: XXX and oooo. Then, recursion is performed until the Interval Length is 1 or 2? DP [I] [J] indicates the maximum length of the Legal parentheses subsequence between [I, j? DP [I] [J] = min {DP [I + 1] [J], DP [I + 1] [k-1] + dp [k + 1] [J] + 1 (I <= k <= J & I and K are a pair of matching brackets)}
Code:
# Include <iostream> # include <string. h> # include <math. h> # include <stdio. h> using namespace STD; # define M 110int DP [m] [m]; char STR [m]; int math (int x, int y) // determine whether to pair. {Return STR [x] = '(' & STR [y] = ') '| STR [x] =' {'& STR [y] ='} '| STR [x] =' ['& STR [y] = ']';} int DFS (int s, int e) {int I, RET, T; If (E <s) return 0; // consider various special cases. If (E = s) return DP [s] [e] = 0; If (E-S = 1) return DP [s] [e] = math (S, e); If (DP [s] [e]! =-1) return DP [s] [E]; // if this status has been calculated. Ret = DFS (S + 1, e); // The posture is incorrect. You can still look at other people's. For (I = S + 1; I <= E; I ++) // find matching parentheses. If (Math (S, I) {T = DFS (S + 1, I-1) + DFS (I + 1, E) + 1; if (T> RET) ret = T;} return DP [s] [e] = ret;} int main () {int I, j, k, n; while (scanf ("% s ", str) {If (STR [0] = 'E') break; n = strlen (STR); memset (DP,-1, sizeof (DP )); DFS (0, n-1); printf ("% d \ n", DP [0] [n-1] * 2);} return 0 ;}

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.