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 ...
随机推荐
- Java学习--多态
1. 多态 多态:同一个对象(实物),在不同时刻体现出来的不同状态 多态的前提: A:要有继承关系 B:要有方法重写 C:要有父类引用指向子类对象 父类 f = new 子类() 多态中的成员访问特点 ...
- Machine learning吴恩达第二周coding作业(选做)
1.Feature Normalization: 归一化的处理 function [X_norm, mu, sigma] = featureNormalize(X) %FEATURENORMALIZE ...
- HTML 标记大全参考手册
1.文件结构 文件类型 <HTML></HTML> (放在文档的开头与结尾) 文件主题 <TITLE></TITLE> (必须放在「文头」区块内) 文头 ...
- jquery加载单文件vue组件
/**注册组件 */ function registerComponent(name){ dm[name] = {}; Vue.component(name + '-component', funct ...
- storm(3)-本机模式-helloworld
pom.xml <dependency> <groupId>org.apache.storm</groupId> <artifactId>storm-c ...
- Python处理文件和查漏
import os,re def change_filename(root): ''' 批量修改excel文件名 ''' for root_,_, fs in os.walk(root): for f ...
- 自学springboot
参考资料 https://www.renren.io/guide/
- Hive动态分区 参数配置及语法
Hive本身是不支持动态分区的.. 但动态分区是真的方便啊..不然手动维护要累死..按日期甚至小时来分区时动辄就好几千上万的分区..手动到哪一年去..? 想要用动态分区要先做一些设置来修改默认的配置. ...
- vue相关问题在工作中的问题及ui组件及html样式搭建相关网站下载资源
https://youzan.github.io/vant/#/zh-CN/nav-bar http://www.builive.com/docs/api/index.html bui框架BUI ...
- 添加ASP.NET网站资源文件夹
ASP.NET应用程序包含7个默认文件夹,分别为Bin.APP_Code.App_GlobalResources.App_LocalResources.App_WebReferences.App_Br ...