[UVa1626]括号序列

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 possibleregular brackets sequence, that contains the given character sequence as a subsequence. Here, a stringa1a2 . . . an is called a subsequence of the string b1b2 . . . bm, if there exist such indices 1 ≤ i1 < i2 <. . . < in ≤ m, that aj = bijfor all 1 ≤ j ≤ n.

Input

The input begins with a single positive integer on a line by itself indicating the number of the casesfollowing, each of them as described below. This line is followed by a blank line, and there is also ablank line between two consecutive inputs.

The input file contains at most 100 brackets (characters ‘(’, ‘)’, ‘[’ and ‘]’) that are situated on asingle line without any other characters among them.

Output

For each test case, the output must follow the description below. The outputs of two consecutive caseswill be separated by a blank line.

Write to the output file a single line that contains some regular brackets sequence that has theminimal possible length and contains the given sequence as a subsequence.

Sample Input

1([(]

Sample Output

()[()]


题意:

定义如下正规括号序列(字符串):

空序列是正规括号序列。

如果S是正规括号序列,那么(S)和[S]也是正规括号序列。

如果A和B都是正规括号序列,那么AB也是正规括号序列。

例如,下面的字符串都是正规括号序列:(), [], (()), ([]), ()[], ()[()],而如下字符串则不是正规括号序列:(, [, ),)(,([)],([]。

输入一个长度不超过100的,由 “(”, “)”, “[”, 和 “]”构成的序列,添加尽量少的括号,得到一个规则序列。如有多解,输出任意一个序列即可。 


题解:

令dp[i][j]表示从i到j的字符串中需要添加括号的数量
if( s[i]!=s[j] ) dp[i][j]=min( dp[i][k]+dp[k+1][j] )
else dp[i][j]=min( dp[i+1][j-1], d[i][j] )


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=105;
int T, n, dp[N][N];
char s[N];

bool Judge( int i, int j ) {
	if( s[i]=='(' && s[j]==')' ) return 1;
	if( s[i]=='[' && s[j]==']' ) return 1;
	return 0;
}

void Print( int l ,int r ) {
	if( l>r ) return;
	if( l==r ) {
		if( s[l]=='(' || s[r]==')' ) printf( "()" );
		else printf( "[]" );
	}
	else if( Judge( l, r ) && dp[l][r]==dp[l+1][r-1] ) {
		printf( "%c", s[l] );
		Print( l+1, r-1 );
		printf( "%c", s[r] );
	}
	else for( int k=l; k<r; k++ )
		if( dp[l][r]==dp[l][k]+dp[k+1][r] ) {
			Print( l, k );
			Print( k+1, r );
			break;
		}
}

int main() {
	scanf( "%d", &T ); getchar();
	while(T--) {
		gets(s+1);
		gets(s+1);
		n=strlen(s+1);
		
		memset( dp, INF, sizeof dp );
		for( int i=1; i<=n; i++ ) dp[i][i]=1;
		
		for( int len=2; len<=n; len++ )
			for( int i=1; i<=n; i++ ) {
				int j=i+len-1;
				if( j>n ) break;
				if( Judge( i, j ) ) {
					if( i+1==j ) dp[i][j]=0;
					dp[i][j]=min( dp[i][j], dp[i+1][j-1] );
					dp[j][i]=dp[i][j];
				}
				for( int k=i; k<j; k++ )
					dp[i][j]=min( dp[i][j], dp[i][k]+dp[k+1][j] ),
					dp[j][i]=dp[i][j];
			}
		Print( 1, n );
		putchar(10);
		if(T) putchar(10);//注意非最后一组的输出末尾要换行
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值