1626 - Brackets sequence——[动态规划]
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
()[()] 紫书上有详解+代码,就不废话了直接贴代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = ;
char s[maxn];
int d[maxn][maxn];
int n;
inline bool match(char a,char b){
if((a=='('&&b==')')||(a=='['&&b==']')) return true;
else return false;
}
void dp(){
for(int i=;i<n;i++){
d[i+][i]=;
d[i][i]=;
} for(int i=n-;i>=;i--){
for(int j=i+;j<n;j++){
d[i][j]=maxn;
if(match(s[i],s[j]))
d[i][j]=min(d[i][j],d[i+][j-]);
for(int k=i;k<j;k++){
d[i][j]=min(d[i][j],d[i][k]+d[k+][j]);
}
}
}
}
void print(int i,int j){
if(i>j) return;
if(i==j){
if(s[i]=='('||s[i]==')') printf("()");
else printf("[]");
return;
}
int ans=d[i][j];
if(match(s[i],s[j])&&ans==d[i+][j-]){
printf("%c",s[i]);print(i+,j-);printf("%c",s[j]);
return;
}
for(int k=i;k<j;k++){
if(ans==d[i][k]+d[k+][j]){
print(i,k);print(k+,j);
return;
}
} }
int main(int argc, const char * argv[]) {
int T;
scanf("%d",&T);
getchar();
while(T--){
getchar();
memset(s, , sizeof s);
char ch;
for(int i=;(ch=getchar())!='\n';i++){
s[i]=ch;
}
n=strlen(s); dp();
print(,n-);
printf("\n");
if(T!=) printf("\n");
}
return ;
}
1626 - Brackets sequence——[动态规划]的更多相关文章
- UVa 1626 Brackets sequence (动态规划)
题意:用最少的括号将给定的字符串匹配,输出最优解.可能有空行. 思路:dp. dp[i][j]表示将区间i,j之间的字符串匹配需要的最少括号数,那么 如果区间左边是(或[,表示可以和右边的字符串匹配, ...
- UVA 1626 Brackets sequence(括号匹配 + 区间DP)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/E 题意:添加最少的括号,让每个括号都能匹配并输出 分析:dp ...
- UVA 1626 Brackets sequence 区间DP
题意:给定一个括号序列,将它变成匹配的括号序列,可能多种答案任意输出一组即可.注意:输入可能是空串. 思路:D[i][j]表示区间[i, j]至少需要匹配的括号数,转移方程D[i][j] = min( ...
- UVa 1626 - Brackets sequence(区间DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA - 1626 Brackets sequence (区间dp)
题意:给定一个串,可能空串,或由'[',']','(',')'组成.问使其平衡所需添加最少的字符数,并打印平衡后的串. 分析:dp[i][j]表示区间(i,j)最少需添加的字符数. 1.递推. #in ...
- POJ 题目1141 Brackets Sequence(区间DP记录路径)
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27793 Accepted: 788 ...
- [caffe]linux下安装caffe(无cuda)以及python接口
昨天在mac上折腾了一天都没有安装成功,晚上在mac上装了一个ParallelDesktop虚拟机,然后装了linux,十分钟就安装好了,我也是醉了=.= 主要过程稍微记录一下: 1.安装BLAS s ...
- [Swift]基础
[Swift]基础 一, 常用变量 var str = "Hello, playground" //变量 let str1="Hello xmj112288" ...
- POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29502 Accepted: 840 ...
随机推荐
- 在n个球中,任意取出m个(不放回),求共有多少种取法
要求: 在n个球中,任意取出m个(不放回),求共有多少种取法 分析: 假设3个球A,B,C,任意取出2个,可分为取出的球中含A的部分和不含A的部分.即AB,AC为一组,BC为一组. 设函数F(n,m) ...
- [Android]Space控件的应用场景
Space控件是在Android 4.0中加入,是个空白的view,一般用于填充View组件中的间隙. support-v4包里提供了兼容低版本的Space控件. 源码分析 Space控件源码非常简单 ...
- Apache Camel继承Spring Boot 实现文件远程复制和转移
pom.xml <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-f ...
- RocksDB 之Write Ahead Log(WAL)
Overview RocksDB 中有三个基本的数据结构概念:memtable, sstfile 和 logfile memtable 是个内存数据结构,新写入会插入memtable 切回选择性地写入 ...
- iOS将image转90,180,270度的方法
http://blog.sina.com.cn/s/blog_6602ffbc0101ckx3.html 这里要分享的是将image旋转,而不是将imageView旋转,原理就是使用quartz2D来 ...
- python LEGB原理简要介绍
- datepicker插件的使用
教程链接:http://www.runoob.com/jqueryui/example-datepicker.html 参数:http://hare6.blog.163.com/blog/static ...
- 简单利用XSS获取Cookie信息实例演示
简单利用XSS获取Cookie信息实例演示 首先要找到一个有XXS的站,这里就不整什么大站了,谷歌一下inurl:'Product.asp?BigClassName',搜出来的命中率也比较高.随便 ...
- 机房收费系统之【只允许一个MDI窗体 错误:426】 标签: vb 2014-08-15 10:36 1149人阅读 评论(23)
机房收费系统的主窗体是MDI窗体,为了在这个窗体上添加控件,所以我们在窗体上添加了picture控件,在MDI窗体中,子窗体实际上位于MDIClient里,即子窗体的父窗体就是MDIClient,而放 ...
- AtCoder Beginner Contest 078 C HSI
虽说这是个水题,但是我做了大概有一个小时吧,才找到规律,刚学概率,还不大会做题. 找到规律后,又想了想,才想到推导过程. 思路:想要知道花费的时间,就要知道提交的次数,我在这里是计算的提交次数的期望, ...