UVA 1626 Brackets sequence ( 矩形dp+输入输出细节 )

本文介绍了一种通过动态规划解决括号序列修复问题的方法。针对给定的不完整括号序列,算法寻找最短的有效括号序列,确保原始序列成为其子序列。文章详细解析了动态规划状态转移方程,并提供了完整的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 a1a2 ...an is called a subsequence of the string b1b2 ...bm, if there exist such indices 1 ≤ i1 < i2 < ... < in ≤ m, that aj = bij for all 1 ≤ j ≤ n. Input The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs. The input file contains at most 100 brackets (characters ‘(’, ‘)’, ‘[’ and ‘]’) that are situated on a single line without any other characters among them. Output For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. 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 1
([(] Sample Output ()[()]
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 113
#define ll long long
#define INF 10000000
using namespace std;
int t,dp[maxn][maxn];
char c;
string seq;
/*
题目大意:给定一个不完整的括号序列, 其中括号类型只有两种,
让求出需要补全的最小添加字符,当然,输出的是补充好的括号序列。

该题的难点一方面是输入输出的坑,输入可以为空串,还可以有空格,所以用scanf会出错,
还有题目要求每个输入之间都有一个空行,所以在循环刚开始都要吃掉一个换行符。

下面就是动态规划的知识了,首先可以知道,d(i,j)=d(i,k)+d(k+1,j);即对于
题目中给定的指标,该字符串可以符合数学性质。
*/

bool Match(char x,char y)
{
    if(x=='('&&y==')') return true;
    if(x=='['&&y==']') return true;
    return false;
}

void print(int i,int j)
{
    if(i>j) return ;
    if(i==j)
    {
        if( seq[i]=='('||seq[i]==')' )  printf("()");
        else printf("[]");
        return ;
    }
    int ans=dp[i][j];
    if( Match(seq[i],seq[j]) && ans==dp[i+1][j-1] )
    {
        printf("%c",seq[i]);
        print(i+1,j-1);
        printf("%c",seq[j]);
        return ;
    }

    for(int k=i;k<=j;k++)
    {
        if(dp[i][k]+dp[k+1][j]==ans)
        {
            print(i,k);
            print(k+1,j);
            return ;
        }
    }
}

int main()
{
    cin>>t;getchar();
    while(t--)
    {
        //puts("");getchar();
        //c=getchar();
        //if(c=='\n') { puts("") ; continue ; }

        //ungetc(c,stdin);
        //scanf("%s",seq);
        getchar();///防止上一个回车符号影响着这次的输入。
        getline(cin,seq);
        //fgets(seq,sizeof(seq),stdin);getchar();
        int len=seq.size();

        memset(dp,0,sizeof(dp));
        for(int i=0;i<len;i++)  { dp[i][i]=1;}

        for(int i=len-2;i>=0;i--)
            for(int j=i+1;j<len;j++)
        {
            dp[i][j]=len;
            if( Match(seq[i],seq[j]) ) dp[i][j]=min(dp[i][j],dp[i+1][j-1]);
            for(int k=i;k<j;k++)  dp[i][j]=min( dp[i][j],dp[i][k]+dp[k+1][j] );
        }
        /*
        for(int p=1;p<len;p++)
        {
             for(int i=0;i<len;i++)
             {
                 if( i+p >= len ) continue;

                 dp[i][i+p]=p+1;
                 if( Match(seq[i],seq[i+p] ) ) dp[i][i+p]=min(dp[i][i+p],dp[i+1][i+p-1]);
                 for( int k=i ; k<=i+p ; k++ )
                     dp[i][i+p] = min ( dp[i][k] + dp[k+1][i+p] , dp[i][i+p] );
                //cout<<i<<" "<<i+p<<" "<<dp[i][i+p]<<endl;
             }
        }
        */
        ///  printf("%d\n",dp[0][len-1]);
        print(0,len-1);puts("");
        if(t) puts("");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值