poj 1141 Brackets Sequence (区间dp)
题目链接:http://poj.org/problem?id=1141
题解:求已知子串最短的括号完备的全序列
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=1e2+;
const int INF=0x3f3f3f3f; int dp[][];
int pos[][];
char str[]; int Find(int x,int y)
{
if((str[x]=='(' && str[y]==')') || (str[x]=='[' && str[y]==']')) return ;
else return ;
} void print(int x,int y)
{
if(x>y) return;
if(x==y)
{
if(str[x]=='[' || str[x]==']') printf("[]");
else printf("()");
}
else
{
if(pos[x][y]>=)
{
print(x,pos[x][y]);
print(pos[x][y]+,y);
}
else if(str[x]=='(')
{
printf("(");
print(x+,y-);
printf(")");
}
else
{
printf("[");
print(x+,y-);
printf("]");
}
}
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%s",str+);
memset(pos,-,sizeof(pos));
memset(dp,,sizeof(dp));
int n=strlen(str+);
for(int i=; i<=n; i++) dp[i][i]=;
for(int d=; d<n; d++)
{
for(int i=; i+d<=n; i++)
{
int j=i+d;
dp[i][j]=INF;
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];
pos[i][j]=k;
}
}
if( Find(i,j) && dp[i+][j-]<dp[i][j] )
{
dp[i][j]=dp[i+][j-];
pos[i][j]=-;
}
}
}
print(,n);
puts("");
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://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...
- 区间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 ...
随机推荐
- 【团队项目选题】自选项目:桌游APP
由于我们团队的黄金点游戏结果是第二名,按理说是一定能选到一个自选项目的,所以以下只列出我们选择的自选项目 自选项目名称 桌游APP 暂定 (名字还没想好) 项目大致介绍 此APP集成了多种桌游,主 ...
- Mybatis传参数
1使用@Param注解传参数 mapper接口:public void updateUser(@Param("user")User user)throws Exception; m ...
- web开发——写一个简单的表格导出操作
一.前台页面: 主要是一个按钮和一个表格,表格有显示数据,按钮负责将表格中的数据选择性地导出.除此外,可以附加一个小窗口和进度条,用于显示下载进度. 1. 按钮:<a href="ja ...
- Web开发技术发展历史
Web开发技术发展历史 来自:天码营 原文:http://www.tianmaying.com/tutorial/web-history Web的诞生 提到Web,不得不提一个词就是"互 ...
- Mac OSX定位命令路径的方法
可以使用which命令来定位一个命令. http://www.cyberciti.biz/faq/how-do-i-find-the-path-to-a-command-file/
- ecshop 支付
支付分成两部分 1.订单信息 2.支付日志ID 3.生成支付代码 一次性支付完成 // 支付信息 include_once('includes/lib_payment.php'); $order['l ...
- php 去掉字符串的最后一个字符
原字符串1,2,3,4,5,6, 去掉最后一个字符",",最终结果为1,2,3,4,5,6 代码如下: $str = "1,2,3,4,5,6,"; $news ...
- 脑筋急转弯——Google 面试
1. 村子里有100对夫妻,其中每个丈夫都瞒着自己的妻子偷情...村里的每个妻子都能立即发现除自己丈夫之外的其他男人是否偷情,唯独不知道她自己的丈夫到底有没有偷情.村里的规矩不容忍通奸.任何一个妻子, ...
- [Data Structure & Algorithm] 七大查找算法
查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找.本文简单概括性的介绍了常见的七种查找算法,说是七种,其实二分查找.插值查找以及斐波那契查找 ...
- IIS连接数、IIS并发连接数、IIS最大并发工作线程数、应用程序池的队列长度、应用程序池的
IIS连接数 一般购买过虚拟主机的朋友都熟悉购买时,会限制IIS连接数,这边先从普通不懂代码用户角度理解IIS连接数 顾名思义即为IIS服务器可以同时容纳客户请求的最高连接数,准确的说应该叫" ...