【动态规划】POJ 1161 & ZOJ1463 & XMU 1033 Brackets sequence
题目链接:
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的更多相关文章
- [poj P1141] Brackets Sequence
[poj P1141] Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Special Judge Description ...
- 区间DP POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29520 Accepted: 840 ...
- 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 ...
- 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 1141 Brackets Sequence(区间DP, DP打印路径)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ 1141 Brackets Sequence (区间DP)
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- 1626 - Brackets sequence——[动态规划]
Let us define a regular brackets sequence in the following way: Empty sequence is a regular sequence ...
随机推荐
- [Javascript] Advanced Console Log Arguments
Get more mileage from your console output by going beyond mere string logging - log entire introspec ...
- [Angular 2] Custom Validtors
Create a custom validtor which only accepts the string start with '123'; function skuValidator(contr ...
- 实现自己的cp命令
1 综述 在Unix和Linux系统里,cp是经常使用的一个命令,用于复制文件,用法如下: $cp src_file dest_file 以下就使用若干系统调用来实现自己的cp. 2 原理 open: ...
- [转] nginx+FastCGI+c++
from: http://www.cnblogs.com/xiaouisme/archive/2012/08/01/2618398.html 一 安装 目的:不需支持php等.就html就行了.步骤: ...
- svcutil 生成代理类时的问题
如果有这个的xsd, group内嵌choice的结构: <xs:complexType name="CreateType"> <xs:sequen ...
- Java NIO中核心组成和IO区别
1.Java NIO核心组件 Java NIO中有很多类和组件,包括Channel,Buffer 和 Selector 构成了核心的API.其它组件如Pipe和FileLock是与三个核心组件共同使用 ...
- redis 记录
参考 : http://keenwon.com/1275.html http://blog.csdn.net/freebird_lb/article/details/7733970 http://w ...
- 尚未解决的intellij问题:补充措施
2016-12-06 遇到问题 D:\software\apache-tomcat-7.0.57\bin\catalina.bat run [2016-12-06 09:54:52,342] Arti ...
- SpringMVC10数据验证
/** * @NotBlank 作用在String * @NotEmpty 作用在集合上 * @NotNull 作用在基本数据类型上 * */ public class User { @NotNull ...
- noip 2013 华容道
/*双向bfs (得分和单项的一样多....)70*/ #include<iostream> #include<cstdio> #include<cstring> ...