HDU 1141---Brackets Sequence(区间DP)

 题目链接

http://poj.org/problem?id=1141

 

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

()[()]

Source

 
 
题意:给了一个括号序列(只有"("  ")"  "["  "]") 现在让添加括号,使括号序列变得匹配,要求添加最少的括号,输出这个匹配的括号序列;
 
思路:区间DP,dp[i][j]表示区间i~j匹配添加括号后区间最小长度,dp[i][j]=dp[i][k]+dp[k+1][j] ,注意当s[i]=='('&&s[j]==')' || s[i]=='['&&s[j]==']' 时,特判一下dp[i][j]=min(dp[i][j],dp[i+1][j-1]+2);  这样可以找出匹配后的序列最小长度,但是题目要求输出匹配的序列,那么可以在定义一个数组v[i][j] 标记i~j区间的断开位置,如果s[i]=='('&&s[j]==')' || s[i]=='['&&s[j]==']' 时 v[i][j]==-1, 然后在递归调用输出即可;
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int inf=0x3f3f3f3f;
char s[105];
int v[105][105];
int dp[105][105];

void print(int l,int r)
{
    if(r<l) return;
    if(l==r)
    {
        if(s[l]=='('||s[l]==')')
            printf("()");
        else
            printf("[]");
        return;
    }
    if(v[l][r]==-1)
    {
        if(s[l]=='(')
        {
            printf("(");
            print(l+1,r-1);
            printf(")");
        }
        else
        {
            printf("[");
            print(l+1,r-1);
            printf("]");
        }
    }
    else
    {
        print(l,v[l][r]);
        print(v[l][r]+1,r);
    }
}

int main()
{
    scanf("%s",s);
    int len=strlen(s);
    memset(dp,0,sizeof(dp));
    for(int i=0; i<len; i++)
        dp[i][i]=2;

    for(int l=1; l<len; l++)
    {
        for(int i=0; i+l<len; i++)
        {
            dp[i][i+l]=inf;
            for(int k=i; k<i+l; k++)
            {
                if(dp[i][i+l]>dp[i][k]+dp[k+1][i+l])
                {
                    dp[i][i+l]=dp[i][k]+dp[k+1][i+l];
                    v[i][i+l]=k;
                }
            }
            if(s[i]=='('&&s[i+l]==')'||s[i]=='['&&s[i+l]==']')
            {
                if(dp[i][i+l]>dp[i+1][i+l-1]+2)
                {
                    dp[i][i+l]=dp[i+1][i+l-1]+2;
                    v[i][i+l]=-1;
                }
            }
        }
    }
    print(0,len-1);
    printf("\n");
    return 0;
}

 

posted @ 2016-09-07 14:53  茶飘香~  阅读(806)  评论(0编辑  收藏  举报