题目链接: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)的更多相关文章

  1. POJ 1141 Brackets Sequence(区间DP, DP打印路径)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  2. poj 1141 Brackets Sequence 区间dp,分块记录

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35049   Accepted: 101 ...

  3. poj 1141 Brackets Sequence ( 区间dp+输出方案 )

    http://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...

  4. 区间DP POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29520   Accepted: 840 ...

  5. POJ 1141 Brackets Sequence (区间DP)

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  6. POJ 1141 Brackets Sequence(括号匹配二)

    题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...

  7. POJ 2955 Brackets (区间dp入门)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  8. POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29502   Accepted: 840 ...

  9. Poj 2955 brackets(区间dp)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7795   Accepted: 4136 Descript ...

随机推荐

  1. RabbitMQ 消息确认机制

    消息确认机制 在之前异常处理部分就已经写了,对于consumer的异常退出导致消息丢失,可以时候consumer的消息确认机制.重复的就不说了,这里说一些不一样的. consumer的消息确认机制 当 ...

  2. php判断中文,英文, 数字

    exeg Warning  This function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0. function checkStr ...

  3. js整数补零

    /* * * 整数前面补零 * * 质朴长存法 * num 要补灵的整数 * n个数,比整数位数多前面自动补零 * **/ function pad(num, n) { var len = num.t ...

  4. ionic 白屏

    昨天在发布新app的时候发现app在高版本的android的时候发现没有问题,在低版本的android 的时候发现存在白屏的情况,在run中alert,不能弹框,run不能运行, 参考这篇文章  ht ...

  5. IP地址,子网掩码、默认网关,DNS服务器是什么意思?

    (一)  问题解析001.   问:  IP地址,子网掩码,默认网关,DNS服务器,有什么区别呀?我知道没有IP地址就不能上网,我也知道没设DNS就不能上外网,可它们都有什么功能,有什么区别呢?还有真 ...

  6. <!DOCTYPE>标签的定义与用法

    <!DOCTYPE> 声明不是 HTML 标签:它是指示 web 浏览器关于页面使用哪个 HTML 版本进行编写的指令. 在 HTML 4.01 中,<!DOCTYPE> 声明 ...

  7. 10月28日PHP基础知识测试题

    本试题共40道选择题,10道判断题,考试时间1个半小时 一:选择题(单项选择,每题2分): 1. LAMP具体结构不包含下面哪种(A) A:Windows系统 B:Apache服务器 C:MySQL数 ...

  8. 百度地图API简单应用

    在做移动端应用时经常用到百度地图API,百度API有强大的示例和文档,开发之前去百度相关网站注册密钥,很块博主只花了几分钟 百度地图API范例 百度地图API文档说明 例子1:输入特定关键字绘制地图标 ...

  9. 为什么为 const 变量重新赋值不是个静态错误

    const 和 let 的唯一区别就是用 const 声明的变量不能被重新赋值(只读变量),比如像下面这样就会报错: const foo = 1 foo = 2 // TypeError: Assig ...

  10. 配置php.ini实现PHP文件上传功能

    本文介绍了如何配置php.ini实现PHP文件上传功能.其中涉及到php.ini配置文件中的upload_tmp_dir.upload_max_filesize.post_max_size等选项,这些 ...