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时,默 ...
随机推荐
- plsql客户端显示菜单等
不小心把plsql的左边的都关了,如图 菜单条---工具---浏览器. 即可.
- form、iframe实现异步上传文件
转载自:http://blog.csdn.net/sunjing21/article/details/4779321 实现主要功能: 页面提供一个上传图片的input file选择框,用于上传某一类型 ...
- IOS - UITableView分批显示数据 实现点击加载更多
Phone屏幕尺寸是有限的,如果需要显示的数据很多,可以先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据.基本上就是数据源里先只放10条, 点击 ...
- 火狐----此地址使用了一个通常用于网络浏览以外的端口。出于安全原因,Firefox 取消了该请求。
FirFox打开80以外的端口,会弹出以下提示: “此地址使用了一个通常用于网络浏览以外的端口.出于安全原因,Firefox 取消了该请求.”.经网上搜索,解决方法如下: 在Firefox地址栏输入a ...
- CSS3的新属性的一下总结
阮一峰:http://www.ruanyifeng.com/blog/2014/02/css_transition_and_animation.html 由于自己经常搞混:animation,tran ...
- IPC进程通信机制
select.poll.epoll之间的区别总结[整理] 进程间通信---共享内存 信号量和互斥锁的区别 http://www.2cto.com/os/201510/445553.html http: ...
- Mac系统下使用VirtualBox虚拟机安装win7--第三步 在虚拟机上安装 Windows 7
第三步 在虚拟机上安装 Windows 7 等待虚拟机进入 Windows 7 的安装界面以后,在语言,货币,键盘输入法这一面,建议保持默认设置,直接点击“下一步”按钮,如图所示
- Delphi中exit、break、continue等跳出操作的区别
Delphi中表示跳出的有break,continue,abort,exit,halt,runerror等 1.break 强制退出最近的一层循环(注意:只能放在循环里:而且是只能跳出最近的一层循环) ...
- MVC4 自定义错误页面(三)
一.概述 MVC4框架自带了定义错误页,该页面位于Shared/Error,该页面能够显示系统未能捕获的异常,如何才能使用该页面: 二.使用步骤: 1.配置WebConfig文件,在System.We ...
- Linux 配置NFS,文件共享
配置: 1.设定共享主机服务器 ---(注意防火墙) 编辑ipA端的/etc/exports 文件 [root@dbrac2 ~]# cat /etc/exports /media 192 ...