poj 1141Brackets Sequence

Brackets Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 31629   Accepted: 9114   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

()[()]

题意:输出完整括号匹配后的字符串


题解:首先将dp[i][j]定义为,从i到j需要最少添加几个字符,vist[i][j]为i地址到j地址中,哪个地址分割,使得两边全部匹配,vist[i][j]为-1时,表示i,j两个位置完整匹配。

然后就是深搜输出字符,如果出现匹配,打印匹配字符,并向他们之间的区间继续深搜,,没出现的话,就在起点到中点的匹配分割点(vist)里面继续搜索。

poj 1141Brackets Sequence_第1张图片


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MAX(a,b) (a) > (b)? (a):(b)
#define MIN(a,b) (a) < (b)? (a):(b)
#define mem(a) memset(a,0,sizeof(a))
#define INF 0x3f3f3f3f
#define MAXN 20005
using namespace std;
#define N 300

int dp[N][N];
char str[N];
int vist[N][N];
int match(int i,int j)
{
    if(str[i]=='('&&str[j]==')')
        return 1;
    if(str[i]=='['&&str[j]==']')
        return 1;
    return 0;
}

void dfs(int s,int n)
{
    if(s>n)
    {
        return ;
    }
    if(s==n)
    {
        if(str[s]=='('||str[n]==')')
            printf("()");
        else
            printf("[]");
    }
    else if(vist[s][n]==-1)
    {
        printf("%c",str[s]);
        dfs(s+1,n-1);
        printf("%c",str[n]);
    }
    else
    {
        dfs(s,vist[s][n]);
        dfs(vist[s][n]+1,n);
    }
}

int main()
{
    while(gets(str)!=NULL)///用scanf会WA(空格)
    {
        memset(dp,0,sizeof(dp));
        int len=strlen(str);
        for(int i=0; i%d\n\n",i,j,vist[i][j]);
                    printf("匹配位置:%d %d\n\n",i,j);
                }
                for(int k=i; kt)
                    {
                        dp[i][x]=t;
                        printf("dp[%d][%d]= ",i,j);
                        cout<%d\n\n",i,j,vist[i][j]);
                    }
                }
            }
        }
        dfs(0,len-1);
        cout<


你可能感兴趣的:(ACM_DP_动态规划)