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 ...
随机推荐
- freemarker模板跟java代码放在一起
在配置 freemarkerConfig时加上 <property name="templateLoaderPath" value="classpath:hello ...
- jxl读取excel
String path=""; String path2=""; File file = new File(path); File file2 = new Fi ...
- sql 列集合
STUFF((SELECT ','+CAST( TYZ_Bh as varchar(10)) FROM #1 where 片区划分='江东' for xml path('')),1,1,'')
- CDQZ Day2
模拟题 day2出题人: liu_runda题目名称 一盘大棋 下一代互联网 强连通分量源程序文件名 chess.cpp net.cpp scc.cpp输入文件名 chess.in net.in sc ...
- 论文阅读 | ExtremeNet:Bottom-up Object Detection by Grouping Extreme and Center Points
相关链接 论文地址:https://arxiv.org/abs/1901.08043 论文代码:https://github.com/xingyizhou/ExtremeNet 概述 ExtremeN ...
- Locust HTTP client
Http client 默认是以安全模式运行的,任何由于连接错误.超时或者类似错误引起的异常,都会返回一个空的Response对象,这个请求将会再locust统计中标记为failure,返回的虚拟对象 ...
- 20190430-Bootstrapの组件
写在前面的乱七八糟:今天务必要把BT盘完~任重道远~ 目录 1.字体图标 2.下拉菜单 3.按钮组 4.输入框组 5.导航 5.1标签页 5.2胶囊式标签页 5.3路径导航/面包屑导航 6.导航条 7 ...
- vscode安装golang插件失败问题
vscode安装golang插件失败问题 dlv go-outline go-symbols gocode-gomod gocode 代码补全 godef 代码跳转 golint gopkgs gor ...
- Service启动流程
Service启动流程从整个宏观上来看,它的模型如下 startService启动流程时序图 Activity中使用的startService方法是定义在Context的抽象类中,它的真正实现者是Co ...
- python namedtuple命名元组
from collections import namedtuple Animal=namedtuple('Animal','name age type') perry=Animal(name='pe ...