题目链接

很早 很早之前就看过的一题,今天终于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)的更多相关文章

  1. 区间DP POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29520   Accepted: 840 ...

  2. poj 1141 Brackets Sequence 区间dp,分块记录

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35049   Accepted: 101 ...

  3. POJ 1141 Brackets Sequence(区间DP, DP打印路径)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  4. POJ 1141 Brackets Sequence (区间DP)

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  5. POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29502   Accepted: 840 ...

  6. poj 1141 Brackets Sequence (区间dp)

    题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...

  7. poj 1141 Brackets Sequence(区间DP)

    题目:http://poj.org/problem?id=1141 转载:http://blog.csdn.net/lijiecsu/article/details/7589877 定义合法的括号序列 ...

  8. poj 1141 Brackets Sequence ( 区间dp+输出方案 )

    http://blog.csdn.net/cc_again/article/details/10169643 http://blog.csdn.net/lijiecsu/article/details ...

  9. POJ 1141 Brackets Sequence(括号匹配二)

    题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...

随机推荐

  1. Avalon学习

    1.认识AvalonAvalon是一个简单易用的迷你的MVVM框架,作者是博客园的司徒正美,去哪儿.搜狐等等都用这个框架.没有任何依赖,兼容性非常好,支持IE6,不到5000行,压缩后不到50KB.官 ...

  2. ArchLinux 安装笔记:续 --zz

    续前话 在虚拟机里调试了几天,终于鼓起勇气往实体机安装了,到桌面环境为止的安装过程可以看我的前一篇文章<ArchLinux 安装笔记>.桌面环境我使用的是 GNOME,虽然用了很长一段时间 ...

  3. SQL SERVER 统计信息概述(Statistics)

    前言 查询优化器使用统计信息来创建可提高查询性能的查询计划,对于大多数查询,查询优化器已经为高质量查询计划生成必要的统计信息,但是在少数情况下,您需要创建附加的统计信息或者修改查询设计以得到最佳结果. ...

  4. Oracle性能优化

    (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE的解析器按照 从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最 ...

  5. 禁用编译器自动生成的函数(Effective C++之06)

    如果想让你的类定义出来的对象是独一无二的,即对象无法被复制,或者使用赋值操作符赋给另外一个对象,那么最好的方法就是禁用拷贝构造函数和赋值操作符.下面介绍几种禁用的方法.(方法来自Effective C ...

  6. CG&CAD resource

    Computational Geometry The Geometry Center (UIUC) Computational Geometry Pages (UIUC) Geometry in Ac ...

  7. Jmeter之安装(一)

    Jmeter Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 小七这边之前用jmeter ...

  8. 关于Vector中的元素中含有指针成员的情况

    对于容器,当容器的各个元素为类类型,且该类类型中含有指针成员时: 如果类类型的析构函数中包含了对指针变量指向内存的释放操作,则在利用clear()函数删除容器所有元素时,会自动调用类的析构函数,自动实 ...

  9. Liferay 6.2 改造系列之十七:当Portlet无权限时,不显示错误信息

    在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Set this to true if users are shown ...

  10. 《DSP using MATLAB》示例Example4.7

    ROC分三种情况: