题目链接:

  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. 第五篇:R语言数据可视化之散点图

    散点图简介 散点图通常是用来表述两个连续变量之间的关系,图中的每个点表示目标数据集中的每个样本. 同时散点图中常常还会拟合一些直线,以用来表示某些模型. 绘制基本散点图 本例选用如下测试数据集: 绘制 ...

  2. [转] nginx+FastCGI+c++

    from: http://www.cnblogs.com/xiaouisme/archive/2012/08/01/2618398.html 一 安装 目的:不需支持php等.就html就行了.步骤: ...

  3. Android 模仿QQ空间风格的 UI(转)

    本文内容 环境 演示模仿QQ空间风格的UI 虽然这个 UI 跟现在的QQ空间有点差别,但是也能学到很多东西. 下载 Demo 环境 Windows 7 64 位 Eclipse ADT V22.6.2 ...

  4. Java基础知识强化之IO流笔记16:IO流的概述和分类

    1. IO流的分类   流向:     (1)输入流:读取数据到内存     (2)输出流:写入数据到硬盘(磁盘)   操作的数据类型:    (1)字节流:操作的数据是字节             ...

  5. WAMP 环境下,YII创建失败 提示 "'php.exe' 不是内部或外部命..."

    现象: http://www.yiichina.com/guide/quickstart.first-app 使用这里的命令  % YiiRoot/framework/yiic webapp WebR ...

  6. js页面加载事件

    <body onload="myfunction()" > </body> <script type="text/javascript&qu ...

  7. sql - 复制表

    1,复制表结构和内容 1)这个表: select * into new_table_name from old_table_name ref:SQL复制数据表及表结构

  8. 安装php时,make步骤报错make: *** [sapi/fpm/php-fpm] Error 1

    安装PHP过程中,make步骤报错:(集中网络上各种解决方法) (1)-liconv -o sapi/fpm/php-fpm /usr/bin/ld: cannot find -liconv coll ...

  9. Active控件有关问题

    ActiveX 控件是允许网站提供视频等内容的网站. 当你浏览 Web 时,它们允许你使用工具栏.股票代号.视频和其它内容. 但是,这些程序有时可能出现问题,或者向你提供不需要的内容. 在某些情况下, ...

  10. Spring mvc 中有关 Shiro 1.2.3 配置问题

    Spring 版本:3.2.x,  4.0.x [问题说明] 首先介绍下配置出错情况: (1)项目中,Spring3 and Spring4 的 applicationContext.xml aop ...