Interval dp:brackets

Source: Internet
Author: User

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 was to find the length of the longest regular brackets Sequenc E is a subsequence of S. That's, you wish to find the largest m such this for indices i1, i2, ..., im where 1≤i1 < I2 < ... < Im≤n, Ai1ai 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 containing only the characters (,), [, and]; Each input test would have 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
((()))
()()()
([]])
)[)(
([][][)
End
Sample Output
6
6
4
0
6

Defines DP [i] [j] as the maximum number of matches for the first I to J brackets in the string
then if we know the maximum match between the I to J interval, then the i+1 to the j+1 interval can be easily obtained.
So if the first and the first J are a pair of matching parentheses then DP [i] [j] = DP [i+1] [j-1] + 2;
then we just need to enumerate all the parentheses in the middle of all I and j from small to large, then satisfy the match with the above formula DP, and then update DP [i] [j] to the maximum value each time. The
method for updating the maximum value is to enumerate the intermediate values of I and J, and then let dp[i] [j] = max (DP [i] [j], DP [i] [f] + DP [f+1] [j]).

 #include <cstdio> #include <cstring> #include <cmath> #include <set>  
#include <cstdlib> #include <iostream> #include <algorithm> using namespace std;  

#define MAXN 202 #define INF 999999 int DP[MAXN][MAXN];  
    int main () {string S;  
        while (Cin>>s) {if (s== "End") is break;  
        Memset (Dp,0,sizeof (DP));  
        int i,j,k,f,len=s.size (); for (I=1; i<len; i++) for (j=0,k=i; k<len; j++,k++)//enumeration interval length, interval endpoint {if (s[ j]== ' (' &&s[k]== ') ') | |  
                    (s[j]== ' [' &&s[k]== '])  
                dp[j][k]=dp[j+1][k-1]+2;  
            for (f=j; f<k; f++)//partition update Interval Dp[j][k]=max (Dp[j][k],dp[j][f]+dp[f+1][k]);  
    }//len-dp[0][len-1] Indicates how many parentheses need to be added to match the success cout<<dp[0][len-1]<<endl;  
} 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.