poj 1141 Brackets Sequence

Brackets Sequence

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 33911   Accepted: 9816   Special Judge

Description

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

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 are regular brackets sequences: 

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

And all of the following character sequences are not: 

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

Some sequence of characters '(', ')', '[', and ']' is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m,that aj = bij for all 1 = j = n.

Input

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

Output

Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.

Sample Input

([(]

Sample Output

()[()] 

Meaning:

Legal parentheses:
1) An empty sequence is a legal sequence.
2) If S is a valid sequence, then both (S) and [S] are valid sequences.
3) If A and B are legal sequences, then AB is a legal sequence.
For example, the sequence given below is a valid bracket sequence:
(),[],(()),([]),()[],()[()],
not valid:
(,[,) ,)(,([)],([(]
outputs a line giving a valid parenthetical sequence with the smallest possible length that contains the given sequence as a subsequence.

Idea:
Let stage r be the length of the subsequence, (1< =r<=n-1), state i is the head pointer of the current subsequence, j=r+i is the tail pointer, if the length of the subsequence is one, dp[i,i]=1; if r is greater than
1 When (s(i)='['&&s(j)==']')||(s(i)=='('&&s(j)==')') then s(i) reaches The minimum number of characters that s(j) needs to add depends on s(i+1) to s(j-1), ie:
dp[i,j]=dp[i+1,j-1]; otherwise, you need to find A transitional intermediate variable, ie dp[i,j]=min(dp[i,k],dp[k=1,j]);

code:
#include<cstdio> 
#include <cstring> 
#include <iostream>
 #define   N 100
 #define INF 0x3f3f3f3f
 using  namespace std;
 char str[N];   // initial string 
int dp[N][N]; // state transition Equation system 
int path[N][N]; // record path 
void print( int i, int j) // recursive output path 
{
     if (i> j)
         return ;    // invalid position return 
    if (i==j )        //If the subsequence contains one character, output matching bracket pair for a single bracket 
    {
         if (str[i]== ' [ ' ||str[i]== ' ] ' )
            printf("[]");
        else
            printf("()");
    }
    else if(path[i][j]==-1)
    {
        printf("%c",str[i]);
        print(i+1,j-1);
        printf("%c",str[j]);
    }
    else
    {
        print(i,path[i][j]);
        print(path[i][j]+1,j);
    }
}

intmain ()
{
    while(gets(str))
    {
        int n= strlen(str);
         if (n== 0 ) // skip blank lines 
        {
            printf("\n");
            continue;
        }
        memset(dp,0,sizeof(dp));
        memset(path,0,sizeof(path));
        for(int  i=0; i<n; i++)
            dp[i][i] = 1 ;
         for ( int d= 1 ; d<n; d++) // enumerate subsequence length 
        {
             for ( int i= 0 ; i<nd; i++) // enumerate subsequence first position 
            {
                 int j=i+d; // tail of subsequence 
                dp[i][j]= INF;  
                 if ((str[i]== ' ( ' &&str[j]== ' ) ' )|| (str[i]== ' [ ' &&str[j]== ' ] '))
                {
                    dp[i][j]=dp[i+1][j-1];
                    path[i][j]=-1;


                }
                for ( int k=i; k<j; k++) // enumerate intermediate pointers 
                {
                     if (dp[i][j]>dp[i][k]+dp[k+ 1 ][j])
                    {
                        dp[i][j]=dp[i][k]+dp[k+1][j];
                        path[i][j] =k; // record path 
                    }
                }
            }
        }
        print(0,n-1);
        printf("\n");
    }
    return 0;
}
 
 

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325313341&siteId=291194637