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 ...
随机推荐
- DropDownList控件
1.DropDownList控件 <asp:DropDownList runat="server" ID="DropDownList1" AutoPost ...
- IntelliJ IDEA 常用设置讲解
说明 IntelliJ IDEA 有很多人性化的设置我们必须单独拿出来讲解,也因为这些人性化的设置让我们这些 IntelliJ IDEA 死忠粉更加死心塌地使用它和分享它. 常用设置 IntelliJ ...
- jQuery下拉菜单插件Tendina.
插件效果: 下载地址和文档: https://github.com/iprignano/tendina
- SDL简介(网络汇总)
摄像头视频播放采用sdl,下面简单介绍下.不保证正确及网址永远有效.后面文章采用tao框架http://sourceforge.net/projects/taoframework/ SDL. ...
- git操作---查询
1.查看git的状态 git status 2.查看git的日志历史记录 git log 3.查看当前git的分支 git branch 4.查看git的配置信息 git config --lis ...
- [Unity] 3D数学基础 - 2D旋转矩阵
2D矩阵的旋转: NewX = X * Cos(α) - Y * Sin(α) NewY = X * Sin(α) + Y * Cos(α) 一般在三角函数中使用的是弧度,我们可以通过下面的公式将角度 ...
- B:Wordpress不同分类调用不同的模板
这里指的是默认文章类型的模板(single.php,category.php) 应用场景: 默认文章默认有2个大类(新闻资讯.游戏资料) 新闻资讯下的所有子分类调用"新闻资讯列表模板,新闻内 ...
- js制作烟花效果
<html> <head> <link type="text/css" rel="stylesheet" href="c ...
- 实现统一用户体验的BaseActivity
对一个规模较大的App开发团队来说,保持统一的代码规范是个好的事情,同时,保持统一的用户体验规范也是个好的事情. 当用户进入一个页面时,一般会有以下交互场景:场景1, 初始化loading,页面从se ...
- Gossip算法
Gossip算法因为Cassandra而名声大噪,Gossip看似简单,但要真正弄清楚其本质远没看起来那么容易.为了寻求Gossip的本质,下面的内容主要参考Gossip的原始论文:<<E ...