POJ 1141 Brackets Sequence(DP)
很早 很早之前就看过的一题,今天终于A了。状态转移,还算好想,输出路径有些麻烦,搞了一个标记数组的,感觉不大对,一直wa,看到别人有写直接输出的。。二了,直接输出就过了。。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int dp[][];
int flag[];
int pos[];
char str[];
int dfs(int L,int R)
{
int i,minz;
if(dp[L][R])
return dp[L][R];
if(L > R)
return dp[L][R] = ;
if(L == R)
return dp[L][R] = ;
if(str[L] == ']'||str[L] == ')')
return dp[L][R] = + dfs(L+,R);
minz = ;
for(i = L; i <= R; i ++)
{
if(str[L] == '('&&str[i] == ')')
minz = min(minz, + dfs(L+,i-) + dfs(i+,R));
else if(str[L] == '['&&str[i] == ']')
minz = min(minz, + dfs(L+,i-) + dfs(i+,R));
else
minz = min(minz, + dfs(L+,i) + dfs(i+,R));
}
return dp[L][R] = minz;
}
void find(int L,int R,int ans)
{
int i;
if(L > R)
return ;
if(L == R)
{
if(str[L] == ')')
printf("()");
else if(str[L] == ']')
printf("[]");
else if(str[L] == '(')
printf("()");
else if(str[L] == '[')
printf("[]");
return ;
}
if(str[L] == ']'||str[L] == ')')
{
if(str[L] == ')')
printf("()");
else if(str[L] == ']')
printf("[]");
find(L+,R,ans-);
return ;
}
for(i = L; i <= R; i ++)
{
if(str[L] == '('&&str[i] == ')')
{
if(ans == + dp[L+][i-] + dp[i+][R])
{
printf("(");
find(L+,i-,dp[L+][i-]);
printf(")");
find(i+,R,dp[i+][R]);
return ;
}
}
else if(str[L] == '['&&str[i] == ']')
{
if(ans == + dp[L+][i-] + dp[i+][R])
{
printf("[");
find(L+,i-,dp[L+][i-]);
printf("]");
find(i+,R,dp[i+][R]);
return ;
}
}
else if(str[L] == '[')
{
if(ans == + dp[L+][i] + dp[i+][R])
{
printf("[");
find(L+,i,dp[L+][i]);
printf("]");
find(i+,R,dp[i+][R]);
return ;
}
}
else if(str[L] == '(')
{
if(ans == + dp[L+][i] + dp[i+][R])
{
printf("(");
find(L+,i,dp[L+][i]);
printf(")");
find(i+,R,dp[i+][R]);
return ;
}
}
}
return ;
}
int main()
{
int len,ans;
scanf("%s",str);
len = strlen(str);
ans = dfs(,len-);
find(,len-,ans);
printf("\n");
return ;
}
POJ 1141 Brackets Sequence(DP)的更多相关文章
- 区间DP POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29520 Accepted: 840 ...
- poj 1141 Brackets Sequence 区间dp,分块记录
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35049 Accepted: 101 ...
- 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)
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29502 Accepted: 840 ...
- poj 1141 Brackets Sequence (区间dp)
题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...
- poj 1141 Brackets Sequence(区间DP)
题目:http://poj.org/problem?id=1141 转载:http://blog.csdn.net/lijiecsu/article/details/7589877 定义合法的括号序列 ...
- poj 1141 Brackets Sequence ( 区间dp+输出方案 )
http://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...
- POJ 1141 Brackets Sequence(括号匹配二)
题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...
随机推荐
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...
- gcc参数-l传递顺序错误导致`undefined reference'的一点小结
刚才编译一个pthread的单文件程序, 使用的命令行是: gcc -o thread1 -lpthread thread1.c 结果报错: $ gcc -o thread1 -lpthread th ...
- 使用html5 canvas绘制圆形或弧线
注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ...
- UVA 10828 Back to Kernighan-Ritchie(高斯消元)
高斯消元求概率 对于非起点,期望x[i] = ∑x[j] / deg[j] #include<cstdio> #include<iostream> #include<cs ...
- Ubuntu / Win7 安装db2 v10.5
抓紧下载v10.5fp1_linuxx64_expc.tar.gz到~/Downloads/java_softcd java_softtar xf v10.5fp1_linuxx64_expc.tar ...
- hdu 4739 2013杭州赛区网络赛 寻找平行坐标轴的四边形 **
是平行坐标轴的,排个序搞一下就行了,卧槽,水的不行 如果不是平行的,则需要按照边长来判断
- 10个很棒的学习Android 开发的网站(转)
看到江湖旅人 写的<10个很棒的学习iOS开发的网站 - 简书>,所以就忍不住写Android 啦,也希望对大家有帮助.我推荐的网站,都是我在学习Android 开发过程中发现的好网站,给 ...
- 在Visualforce page中用自带的控件实现Ajax回调后台方法(并且可以用js去动态给parameters赋值)
这里用的组合是:apex:commandLink + apex:actionFunction + apex:outputPanel 这里的 apex:commandLink 和 apex:actio ...
- 【zTree】 zTree使用的 小例子
使用zTree树不是第一次了 但是 还是翻阅着之前做的 对照着 使用起来比较方便 这里就把小例子列出来 总结一下使用步骤 这样方便下次使用起来方便一点 使用zTree树的步骤: 1.首先 在 ...
- 【微信Java开发 --1---番外1】在windows下,使用JAVA执行多条DOS命令+文件夹/路径中有空格怎么解决【目的是实现内容穿透外网】
内网穿透外网的那一篇,参正集1 但是每次都要Ctrl+R 启动DOS窗口,也就是CMD,一句一句的去粘,略显繁琐. 所以将这些任务写在JAVA程序中,启动一次程序就可以实现[内网穿透]的功能,多好啊! ...