UVA - 1626 Brackets sequence ( dp )

原题:

Let us defie 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 fid the shortest possible
regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string
a 1a 2. . . a n is called a subsequence of the stringb 1b 2. . . b m, if there exist such indices1 i 1 < i2 <
. . . < inm, thataj= bij for all1 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 fie 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 fie 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[i][j]表示字串s[i][j]至少需要添加多少个括号,当s[i]和s[j]匹配时,dp[i] [j]=min{ dp[i] [j] , dp[i+1] [j-1] }。dp[i] [j]=min{ dp[i] [j] , dp[i] [k]+dp[k+1] [j] | i<=k<j }。题目还有两个坑点,一个是可能输出空串,并且输入空串时还应对应输出一个空串;另一个是每个相邻样例间要有一个输出一个空行,并且最后一个样例后不输出空行。导致WA了好几遍……


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <deque>
#include <string>
#include <cmath>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <climits>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define INF 2147483647
using namespace std;
typedef long long ll;
int t;
string s;
int n,dp[105][105];
bool match(string ss,int a,int b)
{
    if(ss[a]=='('&&ss[b]==')')
        return true;
    else if(ss[a]=='['&&ss[b]==']')
        return true;
    else
        return false ;
}
void print(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,j)&&ans==dp[i+1][j-1])
    {
        cout<<s[i];
        print(i+1,j-1);
        cout<<s[j];
        return ;
    }
    for(int k=i;k<j;k++)
        if(ans==dp[i][k]+dp[k+1][j])
    {
        print(i,k);
        print(k+1,j);
        return ;
    }

}
int main()
{
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        getchar();
        s.clear();
        //cin>>s;
        getline(cin,s);
        n=s.size();
        memset(dp,0,sizeof(dp));
        for(int i=0; i<n; i++)
            dp[i][i]=1;
        for(int i=n-2; i>=0; i--)
            for(int j=i+1; j<n; j++)
            {
                dp[i][j]=n;
                if(match(s,i,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]);
            }
        print(0,n-1);
        printf("\n");
        if(t)//最后一个样例不输出空行
            printf("\n");
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值