Brackets sequence
题意:
给你一个括号序列(有中小括号),求出以给定序列为子序列的最小合法括号序列。
分析:
非常经典,以前做过相似一道题,用区间dp,但怎么把这个序列求出来没想出来。
dp[i][j]表示区间i-j是序列合法要增加括号的最小数量,并pos[i][j]表示i-j在哪个位置断开最小,最后通过递归位置打印出答案。
此题序列可能是空串。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int dp[][],pos[][],len;
char s[];
void solve(){
memset(dp,,sizeof(dp));
for(int i=;i<len;++i)
dp[i][i]=;
for(int l=;l<len;++l)
for(int i=;i+l<len;++i){
int j=i+l;
int minv=INF;
for(int k=i;k<j;++k){
if(dp[i][k]+dp[k+][j]<minv)
{
minv=dp[i][k]+dp[k+][j];
pos[i][j]=k;
}
}
if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']'){
if(dp[i+][j-]<minv){
minv=dp[i+][j-];
pos[i][j]=-;
}
}
dp[i][j]=minv;
}
}
void dfs(int l,int r){
if(l>r)return;
if(l==r){
if(s[l]=='('||s[l]==')')
printf("()");
else printf("[]");
}
else{
if(pos[l][r]>=){
dfs(l,pos[l][r]);
dfs(pos[l][r]+,r);
}
else{
if(s[l]=='('){
printf("(");
dfs(l+,r-);
printf(")");
}
else{
printf("[");
dfs(l+,r-);
printf("]");
}
}
}
}
int main()
{
int ca=,t;
scanf("%d",&t);
getchar();
while(t--){
gets(s);
gets(s);
memset(pos,-,sizeof(pos));
len=strlen(s);
if(len==)
{
printf("\n");
if(t)printf("\n");
continue;
}
solve();
dfs(,len-);
printf("\n");
if(t)printf("\n");
}
return ;
}
Brackets sequence的更多相关文章
- POJ 题目1141 Brackets Sequence(区间DP记录路径)
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27793 Accepted: 788 ...
- POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29502 Accepted: 840 ...
- POJ1141 Brackets Sequence
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...
- ZOJ1463:Brackets Sequence(间隙DP)
Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...
- poj 1141 Brackets Sequence 区间dp,分块记录
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35049 Accepted: 101 ...
- [poj P1141] Brackets Sequence
[poj P1141] Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Special Judge Description ...
- CSUOJ 1271 Brackets Sequence 括号匹配
Description ]. Output For each test case, print how many places there are, into which you insert a ' ...
- POJ 1141 Brackets Sequence(区间DP, DP打印路径)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- 区间DP POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29520 Accepted: 840 ...
随机推荐
- Code::Blocks生成的EXE文件执行错误解决:The program can't start because libgcc_s_dw2-1.dll is missing
想用C++弄个简单东东,看有没有可行性, 开发软件,微软的太大太肿,就选用了Code::Blocks. 测试HELLO时,在工程环境中没问题的,但生成的EXE执行有问题, 报什么 libgcc_s_d ...
- hdu 3579 Hello Kiki
不互质的中国剩余定理…… 链接http://acm.hdu.edu.cn/showproblem.php?pid=3579 #include<iostream>#include<st ...
- http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html(重要)
http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html
- 关于JS中变量的作用域-实例
先看问题,如下,自己运行一下吧! if (!('_qyzA' in window)) { var _qyzA = 1; } alert(_qyzA);//undefined 分析:首先,所有的全局变量 ...
- Windows下gcc以及Qt的DLL文件调用之总结(三种方法)
DLL与LIB的区别 :1.DLL是一个完整程序,其已经经过链接,即不存在同名引用,且有导出表,与导入表lib是一个代码集(也叫函数集)他没有链接,所以lib有冗余,当两个lib相链接时地址会重新建立 ...
- Quartz的任务的临时启动和暂停和恢复
Quartz的任务的临时启动和暂停和恢复 在项目中需要手动启停某些服务,那么需要有一个控制这些任务的类.由于任务是有Quartz控制的,我们只需要通过Quartz的相关的API实现相关的功能即可. p ...
- 图像二值化----otsu(最大类间方差法、大津算法)
最大类间方差法是由日本学者大津于1979年提出的,是一种自适应的阈值确定的方法,又叫大津 法,简称OTSU.它是按图像的灰度特性,将图像分成背景和目标2部分.背景和目标之间的类间方差越大,说明构成图像 ...
- Linux设备管理之权限倾斜——mem、proc、devfs、sysfs、udev(下)
linux发展第一阶段 01devfs(linux2.6之前) 02udev(用户空间) 03sysfs(linux2.6之后,描述设备属性) linux发展第二阶段 01sysfs+udev(ude ...
- How to Determine the Version of Oracle XML Publisher for Oracle E-Business Suite 11i and Release 12 (Doc ID 362496.1)
Modified: 29-Mar-2014 Type: HOWTO In this DocumentGoal Solution 1. Based upon an output file gen ...
- mysqldump批量导出(多张表)表结构及表数据
Mysql 批量导出表结构(数据) 仅导出结构,不导出数据: 1.导出數據库為dbname的表结构 mysqldump -h主机地址 -u用户名 -p密码 -d dbname >db.s ...