链接

dp好想  根据它定义的 记忆化下就行

路径再dfs一遍 刚开始以为要判空格 所以加了判空格的代码 后来知道不用 。。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define INF 0xffffff
int dp[][];
char s[],q[];
int dfs(int a,int b)
{
int i,d1,d2 = INF;
if(dp[a][b]!=-)
return dp[a][b];
if(a==b)
return dp[a][b] = ;
if(a>b)
return dp[a][b] = ;
if(s[a]==q[s[b]])
d1 = dfs(a+,b-);
else
{
int x,y;
x = dfs(a+,b)+;
y = dfs(a,b-)+;
d1 = min(x,y);
}
for(i = a ; i < b ; i++)
d2 = min(d2,dfs(a,i)+dfs(i+,b));
dp[a][b] = min(d1,d2);
return dp[a][b];
}
void find(int a,int b)
{
int i;
if(a==b)
{
if(s[a]=='('||s[a]==')')
printf("()");
else if(s[b]=='['||s[a]==']')
printf("[]");
return ;
}
if(a>b)
return ;
if(s[a]==q[s[b]]&&dp[a][b]==dp[a+][b-])
{
if(s[a]=='(')
{
printf("(");
find(a+,b-);
printf(")");
}
else if(s[a]=='[')
{
printf("[");
find(a+,b-);
printf("]");
}
}
else
if(dp[a][b]==(dp[a+][b]+))
{
if(s[a]=='('||s[a]==')')
printf("()");
else if(s[a]=='['||s[a]==']')
printf("[]");
find(a+,b);
}
else
if(dp[a][b]==dp[a][b-]+)
{
find(a,b-);
if(s[b]=='('||s[b]==')')
printf("()");
else if(s[b]=='['||s[b]==']')
printf("[]");
}
else
{
for(i = a ; i < b ; i++)
{
if(dp[a][b]==dp[a][i]+dp[i+][b])
{
find(a,i);
find(i+,b);
break;
}
}
}
}
int main()
{
int k;
q[')'] = '(';
q[']'] = '[';
//q[' '] = ' ';
while(gets(s)!=NULL)
{
memset(dp,-,sizeof(dp));
k = strlen(s);
int ans = dfs(,k-);
find(,k-);
puts("");
}
return ;
}

poj1141Brackets Sequence(dp+路径)的更多相关文章

  1. DP+路径 URAL 1029 Ministry

    题目传送门 /* 题意:就是从上到下,找到最短路,输出路径 DP+路径:状态转移方程:dp[i][j] = min (dp[i-1][j], dp[i][j-1], dp[i][j+1]) + a[[ ...

  2. POJ1015 && UVA - 323 ~Jury Compromise(dp路径)

    In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of ...

  3. Codeforces Round #598 (Div. 3)E(dp路径转移)

    题:https://codeforces.com/contest/1256/problem/E 题意:给一些值,代表队员的能力值,每组要分3个或3个以上的人,然后有个评价值x=(队里最大值-最小值), ...

  4. POJ 1141 Brackets Sequence(DP)

    题目链接 很早 很早之前就看过的一题,今天终于A了.状态转移,还算好想,输出路径有些麻烦,搞了一个标记数组的,感觉不大对,一直wa,看到别人有写直接输出的..二了,直接输出就过了.. #include ...

  5. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  6. poj2264 dp+路径

    //Accepted 208K 0MS //dp //最长公共子序列+路径 #include <cstdio> #include <cstring> #include < ...

  7. Codeforces Round #277 (Div. 2) E. LIS of Sequence DP

    E. LIS of Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/pr ...

  8. URAL1029. Ministry(DP+路径)

    链接 路径麻烦啊 很多细节 倒回去搜一遍 卡了一节数据库.. #include <iostream> #include<cstdio> #include<cstring& ...

  9. [IOI1999]花店橱窗布置(DP路径记录)

    题目:[IOI1999]花店橱窗布置 问题编号:496 题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V ...

随机推荐

  1. Intellij IDEA配置优化--转载

    1. 在线激活 安装IntelliJ IDEA 2016.1.2版本后,在联网状态下激活.Help --> Register,选择lisence server,粘贴地址http://www.it ...

  2. (poj)3268 Silver Cow Party 最短路

    Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...

  3. [技术翻译] 构建现代化的Objective-C (下)

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3563880.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  4. 以boost::function和boost:bind取代虚函数

    转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...

  5. Demo02_对结构体进行文件读写_张仕传_作业_

    #include <iostream> using namespace std; #define StructArrarySize 5 // 老师数量 #define StudentNum ...

  6. 关于MessageBox的用法

    今天编写MFC工程的时候,使用MessageBox函数,老是出错,不断从网上查找解决方案,最后找到了 MessageBox( _T("Help, Something went wrong.& ...

  7. jquery 100%全屏自适应宽可点击左右和焦点的自动切换幻灯片特效

    http://www.divcss5.com/css-texiao/texiao717.shtml http://d.divcss5.com/divcss5/down/2014062201.zip

  8. Android NFC标签 开发深度解析 触碰的艺术

    有几天没有更新博客了,不过本篇却准备了许久,希望能带给每一位开发者最简单高效的学习方式.废话到此为止,下面开始正文. NFC(Near Field Communication,近场通信)是一种数据传输 ...

  9. ecshop会员中心增加订单搜索功能

    在user.php中的act=order_list中增加以下程序. $order_sn = isset($_REQUEST['order_sn'])?$_REQUEST['order_sn']:''; ...

  10. Universal-Image-Loader(UIL)使用方法&流程图&源码分析 ----- 未完

    GitHub源码: Android-Universal-Image-Loader Features Multithread image loading (async or sync) 多线程加载(同步 ...