poj 1141 Brackets Sequence ( 区间dp+输出方案 )
http://blog.csdn.net/cc_again/article/details/10169643
http://blog.csdn.net/lijiecsu/article/details/7589877
如果有空串要用gets,scanf不能处理空串
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue const int MAXN = + ; int n,m; int dp[][],path[][];
char a[]; bool match(int i,int j)
{
return (a[i]=='(' && a[j]== ')') || (a[i]=='[' && a[j]== ']');
}
void print(int i,int j)
{
if(i>j) return;
if(i == j)
{
if(a[i] == '(' || a[i]== ')')
pf("()");
else
pf("[]");
return;
}
if(path[i][j] == -)
{
pf("%c",a[i]);
print(i+,j-);
pf("%c",a[j]);
return;
}
else
{
print(i,path[i][j]);
print(path[i][j]+,j);
}
} int main()
{
int i,j;
while(gets(a))
{
n = strlen(a);
mem(dp,);
mem(path,);
for(i=;i<n;i++)
dp[i][i]=;
for(int l = ;l<n;l++)
{
for(i=;i<n-l;i++)
{
j = i+l;
dp[i][j] = <<;
if(match(i,j) && dp[i+][j-] < dp[i][j])
{
dp[i][j] = dp[i+][j-];
path[i][j] = -;
} for(int k =i;k<j;k++)
{
if(dp[i][k]+dp[k+][j] < dp[i][j])
{
dp[i][j] = dp[i][k]+dp[k+][j];
path[i][j] = k;
}
}
}
}
print(,n-);
blank;
}
return ;
}
poj 1141 Brackets Sequence ( 区间dp+输出方案 )的更多相关文章
- 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,分块记录
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35049 Accepted: 101 ...
- poj 1141 Brackets Sequence (区间dp)
题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...
- 区间DP POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29520 Accepted: 840 ...
- POJ 1141 Brackets Sequence (区间DP)
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- POJ 1141 Brackets Sequence(括号匹配二)
题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...
- POJ 2955 Brackets (区间dp入门)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29502 Accepted: 840 ...
- Poj 2955 brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7795 Accepted: 4136 Descript ...
随机推荐
- xshell远程连接虚拟机
xshell百度网盘下载地址: 链接: https://pan.baidu.com/s/1cNn458wUyKNOcAxQ8vEPQg密码: 8vrw 安装xshell,步骤很简单这里就不多说了 if ...
- jquery事件三 -- load(), ready(), resize()以及bind()事件
例子1 ready() DOM加载完毕 load() 元素加载完毕 resize() 浏览器窗口的大小发生变化 <!DOCTYPE html> <html lang="en ...
- js,timeout,promise执行顺序
总结 macro-task包括:script(整体代码), setTimeout, setInterval, setImmediate, I/O, UI rendering. micro-task包括 ...
- nginx高性能WEB服务器系列之九--nginx运维故障日常解决方案
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- python 入门级教你如何拿到小姐姐微信
第一题: 首先错误的思路,首先找出 707829217/2+1 里面的所有奇数,然后再利用两个for,来判断 import math def func_get_prime(n): return ...
- 返回参数去掉xml格式,以纯json格式返回(转)
Json 格式显示public static void Register(HttpConfiguration config) { //////////////设置不以xml格式返回 config.Fo ...
- archlinux安装串口终端ckermit
1. 将usb转串口连接到PC上.通过dmesg命令可以查看USB转串口是否被PC识别. 显示 ……attachec to ttyUSB0即被识别. linux系统一般有USB转串口驱动(PL ...
- VS中的DataPager分页
微软的DataPager分页功能很强大,不要设置数据库存储过程,只要添加个DataPager控件,关联下要分页的控件,简单设置就可以有不错的分页效果.当然要有更理想的效果还是要前台和后台处理下. wi ...
- vue-cli项目启动遇到的坑
利用 npm init webpack projectname 之后 切换到项目所在文件夹下,执行命令 npm install ,一直非常慢,卡在那里基本不动. 最后是利用cnpm 安装成功的. 转载 ...
- vs2017创建netcore项目,部署到linux的docker容器里面
开发环境 1.win10下面安装VS2017 2.linux安装Ubuntu16.4系统 步骤: 第一步:linux安装docker容器 docker中文文档,里面有详解的docker介绍及讲解,建议 ...