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 ...
随机推荐
- org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:
详细错误堆栈信息: org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" ...
- hdu 4412 2012杭州赛区网络赛 期望
虽然dp方程很好写,就是这个期望不知道怎么求,昨晚的BC也是 题目问题抽象之后为:在一个x坐标轴上有N个点,每个点上有一个概率值,可以修M个工作站, 求怎样安排这M个工作站的位置,使得这N个点都走到工 ...
- hdu 4041 2011北京赛区网络赛B 搜索 ***
直接在字符串上搜索,注意逗号的处理 #include<cstdio> #include<iostream> #include<algorithm> #include ...
- linux SecureCRT ssh key认证登陆
转自:http://blog.chinaunix.net/uid-20639775-id-3207171.html 通过SecureCRT创建key登录认证 一.生成公钥/密钥对 使用SecureCR ...
- WINDOWS页式内存管理解析
jpg 改 rar
- DotNet 资源大全中文版,内容包括:编译器、压缩、应用框架、应用模板、加密、数据库、反编译、IDE、日志、风格指南等
DotNet 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-dotnet 是由 quozd 发起和维护.内容包括:编译器. ...
- JAVA多线程实现的四种方式
Java多线程实现方式主要有四种:继承Thread类.实现Runnable接口.实现Callable接口通过FutureTask包装器来创建Thread线程.使用ExecutorService.Cal ...
- MPAndroidChart饼图属性及相关设置
公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图: 布局文件: &l ...
- 【转】Kylin的cube模型
转自:http://www.cnblogs.com/en-heng/p/5239311.html 1. 数据仓库的相关概念 OLAP 大部分数据库系统的主要任务是执行联机事务处理和查询处理,这种处理被 ...
- JVM参数调优
JVM参数调优 JVM参数调优是一个很头痛的问题,可能和应用有关系,下面是本人一些调优的实践经验,希望对读者能有帮助,环境LinuxAS4,resin2.1.17,JDK6.0,2CPU,4G内存,d ...