POJ 1141 Brackets Sequence (interval dp)

Source: Internet
Author: User

Language:DefaultBrackets Sequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26424 Accepted: 7451 Special Judge

Description

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.

Sample Input

([(]

Sample Output

()[()]

Source

Northeastern Europe 2001

Interval DP





#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace STD; #define N 305#define INF 0x3f3f3f3fint Dp[n][n],vis[n][n];char a[n];inline bool Judge (int I,int j) {if (a[i]== ' (' & &a[j]== ') ' | | a[i]== ' [' &&a[j]== '] ') return True;return false;} void print (int i,int j) {if (i>j) return; if (i==j) {if (a[i]== ' | | | a[i]== ') printf ("()"); elseprintf ("[]"); return;}    if (vis[i][j]==-1) {printf ("%c", A[i]);p rint (i+1,j-1);p rintf ("%c", A[j]); return;}    Print (I,vis[i][j]); Print (VIS[I][J]+1,J);} int main () {int i,j;while (gets (a)) {int Len=strlen (a); if (len==0) {printf ("\ n"); continue;} Memset (Dp,0,sizeof (DP)); memset (vis,-1,sizeof (VIS)); for (i=0;i<len;i++) dp[i][i]=1;for (i=len-1;i>=0;i--) for (j=i+1;j<len;j++)         {dp[i][j]=dp[i+1][j]+1;            At first it was self-matching, that is, adding a vis[i][j]=i; if (judge (I,J))//If the end-to-end match, this special processing {if (Dp[i][j]>dp[i+1][j-1]) {dp[i][j]=dp[i+1][j-1];vis[i][j]=-1;}} for (int k=i+1;k<j;k++)//i~j, there is a match betweenif (judge (I,k)) {if (Dp[i][j]>dp[i][k]+dp[k+1][j]) {dp[i][j]=dp[i][k]+dp[k+1][j];vis[i][j]=k;}}} Print (0,len-1);p rintf ("\ n");} return 0;}


POJ 1141 Brackets Sequence (interval 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.