uva1626 Brackets sequence

Brackets sequence

Time Limit: 4500MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
[Submit] [Go Back] [Status] 

Description
Download as PDF
Let us define a regular brackets sequence in the following way:
Empty sequence is a regular sequence.
If S is a regular sequence, then (S) and [S] are both regular sequences.
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

()[()]

题意:给出一串括号字符,要求补上最少的括号是括号完全匹配,'('与‘)’匹配,‘[’和‘]’匹配。

DP,dp[i][j]表示i-j这段要完全匹配需要最少补上几个。。

状态转移方程是。。如果i和j匹配,dp[i][j]=min(dp[i][j],dp[i+1][j-1]).  意思就是i和j匹配直接找i到j这段和i+1到j-1这段的最小值就可以

还有一个是 dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]),把i到j分成两段,找最小。。

输出看代码。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=1<<30;
int dp[110][110];
bool match(char a,char b)
{
    if(a=='('&&b==')')
        return 1;
    if(a=='['&&b==']')
        return 1;
    return 0;
}
char s[110];
void prin(int i,int j)
{
    if(i>j)
        return;
    if(i==j)
    {
        if(s[i]=='('||s[i]==')')
            printf("()");
        else
            printf("[]");
        return;
    }
    int ans=dp[i][j];
    if(match(s[i],s[j])&&ans==dp[i+1][j-1])
    {
        printf("%c",s[i]);
        prin(i+1,j-1);
        printf("%c",s[j]);
        return;
    }
    for(int k=i;k<j;k++)
    {
        if(ans==dp[i][k]+dp[k+1][j])
        {
            prin(i,k);
            prin(k+1,j);
            return;
        }
    }
}
int main()
{
    int t,i,n,j,flag=0;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        gets(s);
        gets(s);
        n=strlen(s);
        if(n==0)
        {
            if(flag++)
                printf("\n");
            printf("\n");
            continue;
        }
        for(i=0;i<n;i++)
        {
            dp[i][i]=1;
        }
        for(i=n-2;i>=0;i--)
        {
            for(j=i+1;j<n;j++)
            {
                dp[i][j]=INF;
                if(match(s[i],s[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]);
                }
            }
        }
        if(flag++)
            printf("\n");
        prin(0,n-1);
        printf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值