poj1141(区间dp基础)

202 篇文章 0 订阅
78 篇文章 0 订阅

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

()[()]

题意:

给出只含有'('')''['']'这四种字符的字符串,要求让这个串匹配的最少需要添加的字符个数并且输出该字符串。

思路:

这题看起来比较水,可是自己想不是太好想,看了别人说要递归自己试了下还行,记忆化搜索写起来挺顺手,不过可惜这题刚开始把状态转移方程搞错了总是wa。

用dp[i][j]表示区间i到j之间的最少的需要匹配的字符对数,状态转移是:

如果x[i]和x[j]匹配,  dp[i][j]=min(dp[i+1][j-1]+1,dp[i][k]+dp[k+1][j]),

否则,  dp[i][j]=min(dp[i][k]+dp[k+1][j])

fen[i][j]表示区间i到j如果dp[i][j]=某一个dp[i][k]+dp[k+1][j],那么fen[i][j]=k表示区间i到j是从k这个地方分开的!而且这个fen[i][j]数组很必要,否则还得在输出的函数里要加上一大堆判断条件!

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
int dp[110][110];
int fen[110][110];
char x[110];
int fdp(int a,int b)
{
    if(dp[a][b]!=-1)return dp[a][b];
    if(a==b)return dp[a][b]=1;
    if(a>b)return dp[a][b]=0;
    int min0=1e9;
    if((x[a]=='('&&x[b]==')')||(x[a]=='['&&x[b]==']'))
        min0=fdp(a+1,b-1)+1;
    for(int k=a; k<b; k++)
        if(min0>fdp(a,k)+fdp(k+1,b))
            min0=fdp(a,k)+fdp(k+1,b),fen[a][b]=k;
    return dp[a][b]=min0;
}
void prin(int a,int b)
{
    if(a==b)
    {
        if(x[a]=='('||x[a]==')')
            printf("()");
        else printf("[]");
        return;
    }
    if(a>b)
        return;
    if(fen[a][b]==-1)
    {
        if(x[a]=='(')
        {
            printf("(");
            prin(a+1,b-1);
            printf(")");
        }
        else
        {
            printf("[");
            prin(a+1,b-1);
            printf("]");
        }
        return ;
    }
    prin(a,fen[a][b]);
    prin(fen[a][b]+1,b);
}
int main()
{
    scanf("%s",x);
    memset(dp,-1,sizeof(dp));
    memset(fen,-1,sizeof(fen));
    int l=strlen(x);
    fdp(0,l-1);
    prin(0,l-1);
    puts("");
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值