POJ 1141 Brackets Sequence——区间dp (括号匹配 + 输出括号)

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

()[()]

思路:

这道题是 HUNNU oj 11757 Brackets 升级版,关于Brackets的题解,可以参考我的另一个博客:HUNNU oj 11757 Brackets————区间dp (括号匹配)

这里我从Brackets的代码基础上加以改造。
这道题的要求是,给一段括号序列,添加最少的括号来使这个序列是匹配的。
我的思路是:先求出给出序列的最长匹配序列,如样例 ([(] 的最长匹配序列就是 [], 即下标为二和四的括号。然后用一个数组flag标记他们。之后将不匹配的(即没有标记的)括号添加其对应的括号让它匹配。然后就是结果。
具体就是在更新dp的同时更新flag就可以了。
flag[i][j][k]表示:区间[i , j]的最长序列的标记数组。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#define MAXN 105
#define INF 0x3f3f3f3f

using namespace std;

void CMP(bool a[], bool b[])
{
    int i;
    for(i = 0; i <= MAXN-1; i++)
    {
        a[i] = b[i];
    }
}

void init(bool a[])
{
    int i;
    for(i = 0; i <= MAXN-1; i++)
    {
        a[i] = false;
    }
}

int main()
{
    bool flag[MAXN][MAXN][MAXN];
    char a[MAXN];
    gets(a);
    int i, j, k, len, ti, n, dp[MAXN][MAXN];
    n = strlen(a);
    memset(dp, 0, sizeof(dp));
    memset(flag, false, sizeof(dp));
    for(len = 2; len <= n; len++)
    {
        for(i = 0; i <= n-1; i++)
        {
            int j = i+len-1;
            if(j > n-1)
                break;
            if((a[i] == '(' && a[j] == ')') || (a[i] == '[' && a[j] == ']'))
            {

                dp[i][j] = dp[i+1][j-1] + 2;
                CMP(flag[i][j], flag[i+1][j-1]);
                flag[i][j][i] = true;
                flag[i][j][j] = true;
            }
            for(k = i; k < j; k++)
            {

                if(dp[i][j] < dp[i][k] + dp[k+1][j])
                {

                    dp[i][j] = dp[i][k] + dp[k+1][j];
                    init(flag[i][j]);
                    for(ti = i; ti <= k; ti++)
                    {
                        flag[i][j][ti] = flag[i][k][ti];
                    }
                    for(ti = k+1; ti <= j; ti++)
                    {
                        flag[i][j][ti] = flag[k+1][j][ti];
                    }
                }
            }
        }
    }
    for(i = 0; i <= n-1; i++)
    {
        if(flag[0][n-1][i] == false)
        {
            if(a[i] == '(')
            {
                printf("%c", a[i]);
                printf(")");
            }
            else if(a[i] == ')')
            {
                printf("(");
                printf("%c", a[i]);
            }
            else if(a[i] == ']')
            {
                printf("[");
                printf("%c", a[i]);
            }
            else if(a[i] == '[')
            {
                printf("%c", a[i]);
                printf("]");
            }
            else
            {
                printf("%c", a[i]);
            }
        }
        else
        {
            printf("%c", a[i]);
        }
    }
    printf("\n");

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值