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 ...
随机推荐
- Pyqt 获取windows系统中已安装软件列表
开始之前的基础知识 1. 获取软件列表 在Python的标准库中,_winreg可以操作Windows的注册表.获取已经安装的软件列表一般是读去windows的注册表: SOFTWARE\Micros ...
- HTML页面实现返回顶部效果 go to top
1.首先导入jQuery插件. 2.js代码: $(window).scroll(function () { if($(window).scrollTop()>=100) { $(". ...
- android 入门-防微信拍摄视频 按钮事件处理
package com.cc.view; import com.cc.R; import com.cc.R.layout; import com.cc.R.menu; import android.o ...
- SQL常见错误及处理方法
1.情况:数据库引擎安装失败,报类似权限不足的错误 解决:可能由于计算机名和用户名相同导致,更改计算机名,卸载干净重装即可
- [J2EE] 在Web如何取得相关路径
来自网络,自己整整一下: request.getRealPath("url"); // 虚拟目录映射为实际目录,不建议使用,使用ServletContext.getRealPath ...
- VR 相关专业词汇
最近在看 SIGGRAPH2015 有关 VR Display and Interaction 的几篇文章,之前从来没看过有关方面的 paper,一看才发现专业词汇太多了,根本不懂啊,幸亏 Paper ...
- Linux3.18.6内核添加系统调用(32位系统)
1.将已经下载好的内核解压,我的目录是 /home/shangsongchao/LinuxKernel/testlinux-3.18.6/linux-3.18.6 2.添加系统调用表: 在/home/ ...
- Python与Hack之Zip文件口令破解
1.需要的库: **import zipfile**import optparse**from threading import Thread(1)zipfile:1.1 zipfile.ZipFil ...
- Swift3.0语言教程获取C字符串
Swift3.0语言教程获取C字符串 Swift3.0语言教程获取C字符串,为了让Swift和C语言可以实现很好的交互,开发者可以使用NSString的cString(using:)方法在指定编码格式 ...
- mybaties 的一些点
resultMap resutType mybaties缓存 待续 mybaties对应关系是bean和数据库字段的对应. 1.mybaties 的返回值是对象的话定义为resultMap=" ...