Zoj1463:brackets Sequence (Gap DP)

Source: Internet
Author: User

Let us define a regular brackets sequence in the following by:

1. Empty sequence is a regular sequence.

2. If S is a regular sequence, then (s) and [s] are both regular sequences.

3. If A and B are regular sequences, then AB is A regular sequence.

For example, all of the following sequences of characters is regular brackets sequences:

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

And all of the following character sequences is not:

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

Some sequence of characters ' (', ') ', ' [', and '] ' is given. You is to find the shortest possible regular brackets sequence, that contains the given character sequence as a Subsequen Ce. Here, a string A1 A2 ... is called a subsequence of the string B1 b2 ... bm, if there exist such indices 1 = I1 < I2 < ... < in = m, which AJ = bij for all 1 = j = N.


Input

The input file contains at most of brackets (characters ' (', ') ', ' [' and '] ') that is situated on a single line without Any other characters among them.


Output

Write to the output file A contains some regular brackets sequence this has the minimal possible length a nd contains the given sequence as a subsequence.


This problem contains multiple test cases!

The first line of a multiple input was an integer N and then a blank line followed by N input blocks. Each input block was in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input

1

([(]


Sample Output

()[()]


The key is in the input and output formats. God pit.

Interval Dp,dp[i][j] Indicates the number of matches between the interval I to J, whether the characters at both ends of the interval match exactly, and if one dp[i][j] = max (dp[i][k]+dp[k+1][j],dp[i+1][j-1]+1) is able to match the state transfer, If not match is dp[i][j] = max (dp[i][j],dp[i][k]+dp[k+1][j]);



#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define UP (I,x,y) for ( i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x;i>=y;i--) #define MEM (A, B) memset (A,b,sizeof (a)) #define W (a) while (a) Char str[105];int t,len,dp[105][105],mark[105][105],pos[105];void dfs (int i,int j) {if (mark[i][j]==-1) {pos[        I]=pos[j]=1;    DFS (I+1,J-1);        } else if (mark[i][j]>=0) {DFS (i,mark[i][j]);    DFS (MARK[I][J]+1,J); } return;    int main () {int l,i,j,k;    scanf ("%d%*c%*c", &t);        while (t--) {gets (str);        Len=strlen (str);            if (!len) {printf ("\ n");            if (t) printf ("\ n");        Continue            } up (i,0,len-1) up (j,0,len-1) {mark[i][j]=-2;        dp[i][j]=0;        } mem (pos,0);        i=j=l=0;                W (L<len) {if (i==j) {i++,j++;         if (J==len)           I=0,l++,j=l;            Continue } if ((str[i]== ' (' &&str[j]== ') ') | |            (str[i]== ' [' &&str[j]== '])                        {Up (k,i,j-1) {if (Dp[i][j]<dp[i][k]+dp[k+1][j]) {                        Mark[i][j]=k;                    DP[I][J]=DP[I][K]+DP[K+1][J];                    }} if (dp[i][j]<dp[i+1][j-1]+1) {mark[i][j]=-1;                dp[i][j]=dp[i+1][j-1]+1; }} else {up (k,i,j-1) {if (dp[i][j]<                        Dp[i][k]+dp[k+1][j]) {mark[i][j]=k;                    DP[I][J]=DP[I][K]+DP[K+1][J];            }}} i++,j++;                if (J==len) {l++;                i=0;            J=l;   }     } dfs (0,len-1);            Up (i,0,len-1) {if (pos[i]==1) printf ("%c", Str[i]); else if (str[i]== ' (' | | |            str[i]== ') printf ("()");        else printf ("[]");        } printf ("\ n");            if (t) {printf ("\ n");        GetChar (); }} return 0;}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Zoj1463:brackets Sequence (Gap DP)

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.