UVA 1626 Brackets Sequence [最优矩阵链乘变式] [线性结构dp]

Brackets sequence
Time Limit: 4500MS 64bit IO Format: %lld & %llu

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. (The last case don’t!)
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(i,j)表示字串S最少需要添加的字符数。
转移有两种情况:

  • 若S是形如[S’]或(S’)的形式,那么dp(i,j)转移到dp(i+1,j-1)
  • 另一种转移是普遍适用的,即S=AB,dp(i,j)转移到dp(i,p)+dp(p+1,j)

那么输出的时候按照dp的顺序判断一下是否相等然后递归输出即可。
!!注意判断i>j要return,因为可能存在像[]这种情况!!
!!还有在读数据的时候有点迷!!数据之间有一行空行,然后对于空串如果用while读的话就死循环了。。。。。。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smin(x,y) (x)=min((x),(y))
#define smax(x,y) (x)=max((x),(y))
#define maxx(x,y,z) max(max(x,y),z)
#define minn(x,y,z) min(min(x,y),z)
const int INF=0x3f3f3f3f;
const int maxn = 105;
int n;
char s[maxn];
inline bool match(char x,char y)
{
                if(x=='(' && y==')') return true;
                if(x=='[' && y==']') return true;
                return false;
}
inline void init()
{
                char ch=getchar();
                int pos=0;
                while(ch^'\n')
                {
                                s[++pos]=ch;
                                ch=getchar();
                }
                n=pos;
                getchar();
}
int dp[maxn][maxn];
void print(int i,int j)
{
                if(i>j) return; // necessary!! 'coz dp(1,2) is unterminated !
                if(i==j)
                {
                                char ch=s[i];
                                if(ch=='('||ch==')') putchar('('),putchar(')');
                                else putchar('['),putchar(']');
                                return;
                }
                int tmp=dp[i][j];
                if(match(s[i],s[j]) && tmp==dp[i+1][j-1])
                {
                                putchar(s[i]);
                                print(i+1,j-1);
                                putchar(s[j]);
                                return;
                }
                for(int p=i;p<j;p++)
                                if(tmp == dp[i][p]+dp[p+1][j])
                                {
                                                print(i,p);
                                                print(p+1,j);
                                                return;
                                }
}
void dynamic()
{
                memset(dp,0x3f,sizeof(dp));
                for(int i=0;i<=n;i++) dp[i+1][i]=0,dp[i][i]=1;
                for(int k=2;k<=n;k++)
                                for(int i=1;i+k-1<=n;i++)
                                {
                                                int j=i+k-1;
                                                if(match(s[i],s[j])) smin(dp[i][j],dp[i+1][j-1]);
                                                for(int p=i;p<j;p++)
                                                                smin(dp[i][j],dp[i][p]+dp[p+1][j]);
                                }
                print(1,n);
}
int main()
{
#ifndef ONLINE_JUDGE
                freopen("seq.in","r",stdin);
                freopen("seq.out","w",stdout);
#endif
                int cas;
                scanf("%d",&cas); getchar(); getchar();
                while(cas--)
                {
                                init();
                                if(n) dynamic();
                                putchar('\n');
                                if(cas)putchar('\n');
                }
                return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值