POJ 2955 Brackets, poj2955brackets

Source: Internet
Author: User

POJ 2955 Brackets, poj2955brackets
Brackets

Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:6622   Accepted:3558
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
SourceStanford Local 2004 question: give you a sequence of parentheses whose length cannot exceed 100, and find the length of the longest legal sequence of parentheses. A valid sequence of parentheses meets the following conditions: 1. the null sequence of parentheses is valid. 2. if a bracket sequence s is valid, both (s) and [s] are valid. 3. if the two parentheses a and B are valid, AB is also valid. 4. other parentheses are invalid. For example: (), [], (), () [], () [()] are legal, and (,],) (, ([)], ([(] is invalid. Solution: a typical topic of the interval DP model. After analyzing the problem, we can find that if we find a pair of matching parentheses, such as [xxx] oooo, we divide the interval into two parts: xxx and oooo. Set dp [I] [j] to indicate the length of the longest legal parentheses subsequence between intervals [I, j]. When I <j, if the interval [I + 1, if no parentheses match with I exist in j, dp [I] [j] = dp [I + 1] [j, so dp [I] [j] = max {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) }. Therefore, we use the length of the entire string as the interval for search, then the last 2 * dp [0] [len-1] is the answer, len represents the length of the string. For details, see the code. Attach the AC code: 1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 using namespace std; 5 const int maxn = 105; 6 char str [maxn]; 7 int dp [maxn] [maxn]; 8 9 bool match (char a, char B) {10 return (a = '(' & B = ') | (a =' ['& B ='] '); 11} 12 13 int dfs (int l, int r) {14 if (l> r) 15 return 0; 16 if (l = r) 17 return dp [l] [r] = 0; 18 if (l + 1 = r) 19 return dp [l] [r] = match (str [l], str [r]); 20 if (dp [L] [r]! =-1) 21 return dp [l] [r]; 22 int ans = dfs (l + 1, r); 23 for (int I = l; I <= r; + + I) 24 if (match (str [l], str [I]) 25 ans = max (ans, dfs (l + 1, I-1) + dfs (I + 1, r) + 1); 26 return dp [l] [r] = ans; 27} 28 29 int main () {30 while (~ Scanf ("% s", str) & str [0]! = 'E') {31 memset (dp,-1, sizeof (dp); 32 int len = strlen (str); 33 dfs (0, len-1 ); 34 printf ("% d \ n", 2 * dp [0] [len-1]); 35} 36 return 0; 37}View Code

 

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.