HDU 1141---Brackets Sequence(区间DP)
题目链接
http://poj.org/problem?id=1141
Description
1. Empty sequence is a regular sequence.
2. If S is a regular sequence, then (S) and [S] are both regular sequences.
3. If A and B are regular sequences, then AB is a regular sequence.
For example, all of the following sequences of characters are regular brackets sequences:
(), [], (()), ([]), ()[], ()[()]
And all of the following character sequences are not:
(, [, ), )(, ([)], ([(]
Some sequence of characters '(', ')', '[', and ']' is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.
Input
Output
Sample Input
([(]
Sample Output
()[()]
Source
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int inf=0x3f3f3f3f;
char s[];
int v[][];
int dp[][]; void print(int l,int r)
{
if(r<l) return;
if(l==r)
{
if(s[l]=='('||s[l]==')')
printf("()");
else
printf("[]");
return;
}
if(v[l][r]==-)
{
if(s[l]=='(')
{
printf("(");
print(l+,r-);
printf(")");
}
else
{
printf("[");
print(l+,r-);
printf("]");
}
}
else
{
print(l,v[l][r]);
print(v[l][r]+,r);
}
} int main()
{
scanf("%s",s);
int len=strlen(s);
memset(dp,,sizeof(dp));
for(int i=; i<len; i++)
dp[i][i]=; for(int l=; l<len; l++)
{
for(int i=; i+l<len; i++)
{
dp[i][i+l]=inf;
for(int k=i; k<i+l; k++)
{
if(dp[i][i+l]>dp[i][k]+dp[k+][i+l])
{
dp[i][i+l]=dp[i][k]+dp[k+][i+l];
v[i][i+l]=k;
}
}
if(s[i]=='('&&s[i+l]==')'||s[i]=='['&&s[i+l]==']')
{
if(dp[i][i+l]>dp[i+][i+l-]+)
{
dp[i][i+l]=dp[i+][i+l-]+;
v[i][i+l]=-;
}
}
}
}
print(,len-);
printf("\n");
return ;
}
HDU 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 ...
- poj 1141 Brackets Sequence ( 区间dp+输出方案 )
http://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...
- UVA 1626 Brackets sequence 区间DP
题意:给定一个括号序列,将它变成匹配的括号序列,可能多种答案任意输出一组即可.注意:输入可能是空串. 思路:D[i][j]表示区间[i, j]至少需要匹配的括号数,转移方程D[i][j] = min( ...
- Ural 1183 Brackets Sequence(区间DP+记忆化搜索)
题目地址:Ural 1183 最终把这题给A了.. .拖拉了好长时间,.. 自己想还是想不出来,正好紫书上有这题. d[i][j]为输入序列从下标i到下标j最少须要加多少括号才干成为合法序列.0< ...
- 区间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(区间DP记录路径)
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27793 Accepted: 788 ...
- ZOJ1463:Brackets Sequence(间隙DP)
Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...
随机推荐
- Dapper sql in
应用场景: 使用 sql的 Case When Then 批量更新某张表,底层数据库用到了Dapper 代码示例: public int UpdateClientReceivedResult(Dict ...
- python学习 变量的操作 与 基本数据类型
一变量(本文使用python3.5): #变量:是计算机内存中的一块区域,变量可以存储规定范围内的值,变量可以改变#在python中变量是计算机内存中数据的引用 python的变量没有明显的类型,具体 ...
- 微软BI 之SSIS 系列 - 使用 SQL Profilling Task (数据探测) 检测数据源数据
开篇介绍 SQL Profilling Task 可能我们很多人都没有在 SSIS 中真正使用过,所以对于这个控件的用法可能也不太了解.那我们换一个讲法,假设我们有这样的一个需求 - 需要对数据库表中 ...
- php的mysql\mysqli\PDO(一)mysql
原文链接:http://www.orlion.ga/1140/ 工作中数据库的操作都被封装好了,这些怎么用的都快忘了干脆写篇博客重新复习下,以后要是再忘记了可以看这篇文章. PHP 5.5.0 起已废 ...
- android studio 使用gradle 导出jar包,并打包assets目录
警告:本文年久失修. 随着android studio的升级 ,gradle的升级,严格按照本文的代码去做可能不会成功,希望依然可以作为解决问题的思路. 最近项目在做一个sdk,供别的开发者使用,所以 ...
- 了解HTML表单之13个表单控件
目录 传统控件 button select option optgroup textarea fieldset legend label 新增控件 datalist keygen output pro ...
- CSS3入门之文本与字体
1.CSS3文本效果 1.1.text-shadow文本阴影 语法:text-shadow: h-shadow v-shadow blur color;(<水平阴影>,<垂直阴影&g ...
- Kruskal算法(二)之 C++详解
本章是克鲁斯卡尔算法的C++实现. 目录 1. 最小生成树 2. 克鲁斯卡尔算法介绍 3. 克鲁斯卡尔算法图解 4. 克鲁斯卡尔算法分析 5. 克鲁斯卡尔算法的代码说明 6. 克鲁斯卡尔算法的源码 转 ...
- Java多线程系列--“JUC线程池”01之 线程池架构
概要 前面分别介绍了"Java多线程基础"."JUC原子类"和"JUC锁".本章介绍JUC的最后一部分的内容——线程池.内容包括:线程池架构 ...
- JavaWeb:实现文件上传
JavaWeb:实现文件上传 理解文件上传: 1.上传文件就是把客户端的文件发送给服务器端. 2.HTTP响应的正文部分最常见的是HTML文档,但是也可以是其他任意格式的数据,如图片和声音文件中的数据 ...