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 ...
随机推荐
- 利用Factory-boy来生成实例数据
库和版本:Faker==2.0.0factory-boy==2.12.0 官方文档:https://factoryboy.readthedocs.io/en/latest/index.html 1. ...
- input的相关兼容性问题
近来在制作登陆页的input文本框和密码框的时候,具体的实例可参考实现带样式的表单验证,我们发现在IE下默认的情况下,input 标签的密码框和文本框宽度不一致,这就尴尬了. 解决这个办法,我们是直接 ...
- Java安全——密钥那些事
标签(空格分隔): Java 安全 概念 密钥是加密算法不可缺少的部分.密钥在安全体系中至关重要,正如其名,私密的钥匙,打开安全的大门.密钥分两种:对称密钥和非对称密钥.非对称密钥里又包含公开密钥和私 ...
- 使用 Swift 构建自定义的ActivityIndicator View
目前在自己的个人项目里,已经开始使用Swift去编写代码.这篇文章把项目中自己设计的一个ActivityIndicator View展示给大家. 在开始之前,我们先看看最终的效果,如下图: 我建议大家 ...
- 微服务开源生态报告 No.5
「微服务开源生态报告」,汇集各个开源项目近期的社区动态,帮助开发者们更高效的了解到各开源项目的最新进展. 社区动态包括,但不限于:版本发布.人员动态.项目动态和规划.培训和活动. 非常欢迎国内其他微服 ...
- MUI - 基于plus.downloader的图片懒加载功能,支持本地缓存
基于plus.downloader的图片懒加载功能,支持本地缓存 简单说一下 在app中,对一些变动不频繁的图片数据(如个人头像等),是需要存储在本地的.我相信这对大多数的app都是强需求的. 怎么使 ...
- Request.Cookies和Response.Cookies
Request.Cookies创建的Cookie只能用于后台不能用于HTML的前台Response.Cookies操作过的Cookie,所有方法获取到的都是被更新过的值,也就是说Response.Co ...
- 猜年龄 v1.0
给定年龄,用户可以猜三次年龄 年龄猜对,让用户选择两次奖励 用户选择两次奖励后可以退出 age = 30 prize_dict = {0: 'durex', 1: 'okamoto', 2: 'Jis ...
- 百度语音识别REST API用法(含JAVA代码)——不须要集成SDK的方法
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zpf8861/article/details/32329457 上一篇文章http://blog.c ...
- 干货 | 解读MySQL 8.0新特性:Skip Scan Range
MySQL从8.0.13版本开始支持一种新的range scan方式,称为Loose Skip Scan.该特性由Facebook贡献.我们知道在之前的版本中,如果要使用到索引进行扫描,条件必须满足索 ...