poj 1141 区间dp+递归打印路径
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 30383 | Accepted: 8712 | Special Judge | ||
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<stdio.h>
#include<string>
#include<cstring>
//#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
char s[maxn];
int dp[maxn][maxn],choose[maxn][maxn];
void printstr(int i,int j)
{
if(i>j)
return ;
if(i==j)
{
if(s[i]=='('||s[i]==')') printf("()");
else printf("[]");
return;
}
if(choose[i][j]==-)
{
printf("%c",s[i]);
printstr(i+,j-);
printf("%c",s[j]);
}
else
{
printstr(i,choose[i][j]);
printstr(choose[i][j]+,j);
}
}
int main()
{
int t;
//scanf("%d",&t);
cin>>s; int len =strlen(s);
for(int i=; i<len; i++)
dp[i][i]=,dp[i+][i]=;
for(int p=; p<len; p++)
{
for(int i=,j=i+p; j<len; i++,j++)
{
dp[i][j]=inf;
choose[i][j]=-;
if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')
dp[i][j]=min(dp[i][j],dp[i+][j-]);
for(int k=i; k<j; k++)
{
if(dp[i][j]>dp[i][k]+dp[k+][j])
{
choose[i][j]=k;
dp[i][j]=dp[i][k]+dp[k+][j];
}
}
}
}
printstr(,len-);
printf("\n");
return ;
}
poj 1141 区间dp+递归打印路径的更多相关文章
- DP(递归打印路径) UVA 662 Fast Food
题目传送门 题意:n个饭店在一条直线上,给了它们的坐标,现在要建造m个停车场,饭店没有停车场的要到最近的停车场,问所有饭店到停车场的最短距离 分析:易得区间(i, j)的最短距离和一定是建在(i + ...
- UVA 1626 区间dp、打印路径
uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...
- POJ 1141 经典DP 轨迹打印
又几天没写博客了,大二的生活实在好忙碌啊,开了五门专业课,每周都是实验啊实验啊实验啊....我说要本月刷够60题,但好像完不成了,也就每天1题的样子.如今写动规还是挺有条理的,包括这道需要打印轨迹,其 ...
- POJ 1141 区间DP
给一组小括号与中括号的序列,加入最少的字符,使该序列变为合法序列,输出该合法序列. dp[a][b]记录a-b区间内的最小值, mark[a][b]记录该区间的最小值怎样得到. #include &q ...
- UVA 531 - Compromise(dp + LCS打印路径)
Compromise In a few months the European Currency Union will become a reality. However, to join th ...
- FatMouse's Speed ~(基础DP)打印路径的上升子序列
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...
- poj 3280(区间DP)
Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7869 Accepted: 38 ...
- poj 2955 区间dp入门题
第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...
- POJ 2955 (区间DP)
题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...
随机推荐
- Java Web开发框架Spring+Hibernate整合效果介绍(附源码)
最近花了一些时间整合了一个SpringMVC+springAOP+spring security+Hibernate的一套框架,之前只专注于.NET的软件架构设计,并没有接触过Java EE,好在有经 ...
- 【STL】next_permutation的原理和使用
1.碰到next_permutation(permutation:序列的意思) 今天在TC上碰到一道简单题(SRM531 - Division Two - Level One),是求给定数组不按升序排 ...
- jQuery中attr()方法用法实例
本文实例讲述了jQuery中attr()方法用法.分享给大家供大家参考.具体分析如下: 此方法设置或返回匹配元素的属性值. attr()方法根据参数的不同,功能也不同. 语法结构一: 获取第一个匹配元 ...
- VS2010编译Qt4.8.2的64版本库
安装qt-win-opensource-4.8.2-vs2010.exe(从http://download.qt.io/archive/qt/4.8/4.8.2/下 载),这个是32位的,里面有编译好 ...
- 常用邮箱的服务器(SMTP/POP3)地址和端口总结
163.com: POP3服务器地址:pop.163.com(端口:110) SMTP服务器地址:smtp.163.com(端口:25) 126邮箱: POP3服务器地址:pop.126.com(端口 ...
- [Android Pro] Android保存图片到系统图库
http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/ http://blog.csdn.net/x ...
- LTS学习
下载 例子 安装部署zookeeper 运行根目录下的sh build.sh或build.cmd脚本 --> 会在dist目录下生成lts-{version}-bin文件夹(bin里有启动命令) ...
- lsof
lsof `which httpd` //那个进程在使用apache的可执行文件 lsof /etc/passwd //那个进程在占用/etc/passwd lsof /dev/hda6 //那个进程 ...
- C/C++学习笔记---高地址、低地址、大段字节序、小段字节序
字节顺序是指占内存多于一个字节类型的数据在内存中的存放顺序,通常有小端.大端两种字节顺序. 小端字节序指低字节数据存放在内存低地址处,高字节数据存放在内存高地址处: 大端字节序是高字节数据存放在低地址 ...
- C#学习笔记----AppDomain应用程序域
使用.Net建立的可执行程序*.exe,并没有直接承载到进程当中,而是承载到应用程序域(AppDomain)当中.应用程序域是.Net引入的一个新概念,它比进程所占用的资源要少,可以被看做是一个轻量级 ...