POJ 1141(Bracket Sequence)-区间DP

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

()[()]

Source

 

 

经典区间DP。。。

/************************************************
* author:crazy_石头
* Pro:POJ 1141 Bracket Sequence
* algorithm:区间DP
* Time:110ms
* Judge Status:Accepted
* 解题思路:
* dp[i][j]表示i~j间最少添加的字符能使所有括号匹配
* 那么当i和j匹配时,i~j的匹配数(dp[i][j])等于i+1~j-1
* 间的匹配数,即dp[i][j]=dp[i+1][j-1];
* 对于一般情况下,dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]
* 不断枚举k即可;找出最小值,用path记录路径,path=-1
* 表示i~j间括号得到匹配,输出递归输出即可;
**************************************************/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>

using namespace std;

#define LL long long
#define rep(i,h,n) for(int i=(h);i<=(n);i++)
#define ms(a,b) memset((a),(b),sizeof(a))
#define INF 1<<29

const int maxn=1500+5;
const int maxm=32000+5;
const LL mod=10000007 ;

int dp[maxn][maxn];//i,j间最少添加几个字符串才匹配;
int path[maxn][maxn];
char str[maxn];

inline bool match(int i,int j)
{
    if((str[i]=='['&&str[j]==']')||(str[i]=='('&&str[j]==')'))
    return true;
    return false;
}

inline void output(int i,int j)
{
    if(i>j)return;
    if(i==j)
    {
        if(str[i]=='['||str[i]==']')printf("[]");
        else printf("()");
        return ;
    }
    if(path[i][j]==-1)//path[i][j]==-1时表示匹配;
    {
        printf("%c",str[i]);
        output(i+1,j-1);
        printf("%c",str[j]);
        return ;
    }

    output(i,path[i][j]);
    output(path[i][j]+1,j);

}

inline void solve()
{
    int len=strlen(str);
    rep(i,0,len-1)
    dp[i][i]=1;
    for(int i=1;i<len;i++)
    {
        for(int j=0;j+i<len;j++)
        {
            int t=j+i;
            dp[j][t]=INF;
            if(match(j,t))
            {
                dp[j][t]=dp[j+1][t-1];
                path[j][t]=-1;
            }
            for(int k=j;k<t;k++)
            {
                if(dp[j][t]>dp[j][k]+dp[k+1][t])
                {
                    dp[j][t]=dp[j][k]+dp[k+1][t];
                    path[j][t]=k;
                }
            }
        }
    }
}

int main()
{
    while(gets(str))
    {
        ms(dp,0);
        ms(path,0);
        int len=strlen(str)-1;
        solve();
        output(0,len);
        printf("\n");
    }
    return 0;
}


 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值