【动态规划】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 ...
随机推荐
- 第一篇:Power BI数据可视化概述
前言 "可视化之工具,可爱者甚蕃.统计学家独爱R,自Python来,世人盛爱matplotlib.余独爱Power BI之出微软而不染(免费),濯Office而不妖(够精简).......& ...
- Camera类
Camera类 使用 Camera 类可从连接到运行 Flash Player 的计算机的摄像头中 捕获视频. 使用 Video 类可在本地监视视频. 使用 NetConnection 和 NetS ...
- ASP.NET-FineUI开发实践-12
1.网上找到了行合并的示例,extjs写的,我把它挪过来改了下,FineUI也能用,就是只能放着看,选择和编辑行扩展列没有测试,放出来大家看着用吧. <script> F.ready(fu ...
- linq的一些用法总结
获取列表数据. IList<Model> list = dao.getmx(Model, pageInfo);//获取数据列表 1.将列表中id一样的数据进行group by分组,并返回序 ...
- Photon的log使用
添加log引用,设置log文件在Photon根目录下的log文件夹内. using ExitGames.Logging;using ExitGames.Logging.Log4Net; public ...
- linux command cp.
Examples cp file1.txt newdir Copies the file1.txt in the current directory to the newdir subdirector ...
- html table 知识点
第一点就是图片在表格中不能对齐边沿的问题,这是和浏览器的渲染模式相关的,在标准模式中是不能对齐的,除非改变CSS某些样式.而在准标准模式和怪异模式中,默认就是对齐的. 这一特性在以前的表格布局时代是大 ...
- 认识html文件基本结构
html文件的结构:一个HTML文件是有自己固定的结构的. <html> <head>...</head> <body>...</body> ...
- Swift - 09 - Optionals
//: Playground - noun: a place where people can play import UIKit // swift中没有被赋值的变量是不能被使用的 //var str ...
- 解决java访问.netWebService的常见问题
到公司没多久,写了一个java调用.net写的webService结果期间用各种方法测试都没有完成,总是抛出异常,最后直接使用SOAP消息去进行调用才成功了,具体代码如下,仅供参考:import ja ...