[UVa1626]括号序列
来源:互联网 发布:软件部署实施方案 编辑:程序博客网 时间:2024/06/01 09:00
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的,由 “(”, “)”, “[”, 和 “]”构成的序列,添加尽量少的括号,得到一个规则序列。如有多解,输出任意一个序列即可。
题解:
#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;}
- uva1626 括号序列
- dp uva1626 括号序列
- [UVa1626]括号序列
- 例题9-10 括号序列 UVa1626
- 区间DP(括号序列,uva1626)
- #UVA1626#Brackets sequence(括号序列---石子归并类Dp)
- NYOJ15 - UVA1626 括号匹配问题(区间dp)
- NYOJ15 - UVA1626 括号匹配问题(dp)
- uva1626
- UVa1626
- UVA1626 - Brackets sequence(区间DP--括号匹配+递归打印)
- 括号序列
- 括号序列
- 括号序列
- 括号序列
- 括号序列
- 括号序列
- 括号序列
- @Cacheable注解在spring3中的使用-实现缓存
- ArrayList源码(2)
- JQuery教程详解二之JQuery效果
- popen函数
- html_day04_字符实体
- [UVa1626]括号序列
- 利用边框制作三角形
- python和java中斜杠的写法及区分
- PAT 1008数组元素循环右移
- raspberry pi 2安装rtl8811/ 8821网卡驱动
- 极乐小程序榜单(第三期)
- LeetCode 232 Implement Queue using Stacks(利用栈实现队列)
- 编程实现删除字符串中所有指定的字符
- CCF201312-2 ISBN号码