[UVa 1626]Brackets sequence

Problem Description

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

()[()]

My Problem Report

一道简单的线性DP,写的时候注意两个Trick

1.串中可能存在空格
2.匹配左右两端时注意有两种不同的括号

My Source Code

//  Created by Chlerry in 2015.
//  Copyright (c) 2015 Chlerry. All rights reserved.
//

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define Size 200
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array, x) memset(array,x,sizeof(array))
typedef pair<int,int> P;
string a;
int f[Size][Size];
bool match(int i,int j)
{
    if(a[i]==' ' && a[j]==' ')
        return 1;
    if(a[i]=='(' && a[j]==')')
        return 1;
    if(a[i]=='[' && a[j]==']')
        return 1;
    return 0;
}
void print(int i,int j)
{
    if(i>j) return;
    if(i==j)
    {
        if(a[i]=='(' || a[i]==')') 
            cout<<"()";
        else if(a[i]=='[' || a[i]==']')
            cout<<"[]";
        else
            cout<<' ';
        return;
    }
    int ans=f[i][j];
    if(match(i,j) && f[i+1][j-1]==ans)
    {
        cout<<a[i];print(i+1,j-1);cout<<a[j];
        return;
    }
    for(int k=i;i<j;k++)
        if(f[i][k]+f[k+1][j]==ans)
        {
            print(i,k);print(k+1,j);
            return;
        }
}
int main()
{
    // freopen("in.txt","r",stdin);
    // freopen("1.txt","w",stdout);
    int T;cin>>T;getline(cin,a);
while(T--)
{
    getline(cin,a);
    getline(cin,a);
    // cout<<"input:'"<<a<<"'"<<endl;
    for(int i=0;i<a.size();i++)
        f[i][i]=(a[i]!=' '),f[i+1][i]=0;
    // printf("f[0][0]=%d\n",f[0][0]);
    for(int i=a.size()-2;i>=0;i--)
        for(int j=i+1;j<a.size();j++)
        {
            f[i][j]=a.size();
            if(match(i,j))
                f[i][j]=f[i+1][j-1];
            for(int k=i;k<j;k++)
                f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]);
            // printf("f[%d][%d]=%d\n",i,j,f[i][j]);
        }
    print(0,a.size()-1);cout<<endl;if(T)cout<<endl;
}
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值