POJ 2955 Brackets (range dp matching)

Source: Internet
Author: User

POJ 2955 Brackets (range dp matching)

 

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

 

Description

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

The empty sequence is a regular brackets sequence, if SIs a regular brackets sequence, then ( S) And [ S] Are regular brackets sequences, andif AAnd BAre regular brackets sequences, then ABIs 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

Question link: http://poj.org/problem? Id = 2955

Question: Give A sequence of parentheses and ask the maximum number of valid parentheses in the sequence. If A is legal, [A] and (A) are legal. If, if B is valid, AB is also valid.

Question Analysis: It is similar to the classical brackets matching in POJ 1141. This question is simpler and we want to solve the problem. Since the maximum number of brackets is required, we need to add the minimum number of brackets, so that the entire sequence is valid, this will be converted into the question 1141, and the maximum matching nature of the brain-dynamic analogy bipartite graph will be opened, the maximum matching + the Maximum Independent Set = points, obviously, if you want to add the least vertices to make the sequence legal, the least vertices to be added will be | the largest independent set |, which we need is the original sequence | the maximum match |, the above is purely yy, the following shows the transfer equation, which is exactly the same as 1141.
Dp [I] [I] = 1;
Then the enumerated range Length
1) peripheral matching: dp [I] [j] = dp [I + 1] [j-1];
2) The peripheral does not match. Enumeration Split points: dp [I] [j] = min (dp [I] [j], dp [I] [k] + dp [k + 1] [j]); (I <= k <j)

#include 
 
  #include 
  
   #include 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]);    }}
  
 

 

Related Article

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.