题目链接:

  http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1033

  http://poj.org/problem?id=1141

  ZOJ目前挂了。

题目大意

  给一个括号序列,要求输出,最少增加括号数情况下,任意一个合法括号序列即可。

  匹配是指()和[]完全合法,可以嵌套。

题目思路:

  【动态规划】

  区间DP,枚举左右区间端点,两种匹配方法:中间拆开匹配或者直接头尾匹配。

  转移得到最优值,同时记录得到最优值的方法,最后逆推得到不用增加的括号位置,再要增加的括号加上即可

  (括号在前在后添加对于答案长短不会有影响)

 //
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 104
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int f[N][N],mark[N][N];
char s[N];
bool c[N];
bool compare(int i,int j)
{
return (s[i]=='(' && s[j]==')')||(s[i]=='[' && s[j]==']');
}
void print(int i,int j)
{
if(i>=j)return;
if(mark[i][j]==-)
{
c[i]=c[j]=;
print(i+,j-);
return;
}
print(i,mark[i][j]);
print(mark[i][j]+,j);
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k,l;
// for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
while(~scanf("%s",s))
// while(~scanf("%d",&n))
{
n=strlen(s);
memset(f,0x7f,sizeof(f));
memset(mark,-,sizeof(mark));
memset(c,,sizeof(c));
for(i=;i<n;i++)
{
f[i][i]=;f[i+][i]=;
}
for(l=;l<n;l++)
{
for(i=,j=i+l;i+l<n;i++,j++)
{
for(k=i;k<j;k++)
{
if(f[i][j]>f[i][k]+f[k+][j])
{
f[i][j]=f[i][k]+f[k+][j];
mark[i][j]=k;
}
}
if(compare(i,j) && f[i][j]>f[i+][j-])
{
f[i][j]=f[i+][j-];
mark[i][j]=-;
}
}
}
print(,n-);
for(i=;i<n;i++)
{
if(!c[i])
{
if(s[i]=='(' || s[i]==')')printf("()");
else printf("[]");
}
else printf("%c",s[i]);
}
puts("");
}
return ;
}
/*
// //
*/

千万不要点

【动态规划】POJ 1161 & ZOJ1463 & XMU 1033 Brackets sequence的更多相关文章

  1. [poj P1141] Brackets Sequence

    [poj P1141] Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K   Special Judge Description ...

  2. 区间DP POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29520   Accepted: 840 ...

  3. POJ 题目1141 Brackets Sequence(区间DP记录路径)

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 788 ...

  4. POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29502   Accepted: 840 ...

  5. ZOJ1463:Brackets Sequence(间隙DP)

    Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...

  6. poj 1141 Brackets Sequence 区间dp,分块记录

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35049   Accepted: 101 ...

  7. POJ 1141 Brackets Sequence(区间DP, DP打印路径)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  8. POJ 1141 Brackets Sequence (区间DP)

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  9. 1626 - Brackets sequence——[动态规划]

    Let us define a regular brackets sequence in the following way: Empty sequence is a regular sequence ...

随机推荐

  1. [CSS3] CSS :target Selector

    The :target selector allows us to interact with a fragment identifier, or hash, in our URL from CSS. ...

  2. 黑白图像(DFS)

    输入一个n*n的黑白图像(1表示黑色,0表示白色),任务是统计其中八连块的个数.如果两个黑格子有公共边或者公共顶点,就说它们属于同一个八连块.如图6-11所示的图形有3个八连块. 图6-11  拥有3 ...

  3. Reso | The Linux Command Line 的中文版

    http://book.haoduoshipin.com/tlcl/book/zh/ 本书是 The Linux Command Line 的中文版, 为大家提供了多种不同的阅读方式. 中英文双语版- ...

  4. POJ 2653 Pick-up sticks (判断线段相交)

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10330   Accepted: 3833 D ...

  5. 异步tcp通信——APM.Server 消息推送服务的实现

    消息推送服务 服务器推送目前流行就是私信.发布/订阅等模式,基本上都是基于会话映射,消息对列等技术实现的:高性能.分布式可以如下解决:会话映射可采用redis cluster等技术实现,消息对列可使用 ...

  6. c# json数据解析——将字符串json格式数据转换成对象

    网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过例子由易到难总结一下处理过程,希望能帮到和我一样开始不会的朋 ...

  7. new Date()在IE,谷歌,火狐上的一些注意项

    1.new Date()在IE浏览器上IE9以上的可以直接使用new Date("yyyy-MM-dd"),但是在IE8上的时候就要使用new Date("yyyy/MM ...

  8. [转载]使用兼容ie6 ie7 ie8 FF的text-overflow:ellips

    使用兼容ie6 ie7 ie8 FF的text-overflow:ellipsis超出文本显示省略号来代替截取函数更有利于seo,如果使用截取函数,源代码中的标题是显示不完整的,即便是在title属性 ...

  9. ie下面兼容性问题的一些总结

    最后一次搞ie兼容性问题,以后都可以不管了0.0 1.浮动兼容性 1.1IE6下的双边距BUG 在IE6下,块元素有浮动和横向margin的时候,最边上元素的横向margin值会被放大成两倍 解决办法 ...

  10. project facet java version 1.6 is not supported

    可能你用的jdk1.5的包,而开发是用的jdk1.6,不允许1.5进行安装 法1,选中项目 Properties , 选择 Project Facets,右击选择 Java , Change Vers ...