[POJ] Brackets Sequence
This problem can be solved elegantly using dynamic programming.
We maintain two arrays:
- cnt[i][j] --- number of parentheses needed to add within s[i..j] inclusively;
- pos[i][j] --- position to add the parenthesis within s[i..j] inclusively.
Then there are three cases:
- cnt[i][i] = 1;
- If s[i] == s[j], cnt[i][j] = cnt[i + 1][j - 1], pos[i][j] = -1 (no need to add any parenthesis);
- If s[i] != s[j], cnt[i][j] = min_{k = i, i + 1, ..., j}cnt[i][k] + cnt[k + 1][j], pos[i][j] = k (choose the best position to add the parenthesis).
After computing cnt and pos, we will print the resulting parentheses recursively.
My accepted code is as follows. In fact, I spent a lot timg on debugging the Wrong Answer error due to incorrect input/output. You may try this problem at this link.
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring> using namespace std; #define INT_MAX 0x7fffffff
#define vec1d vector<int>
#define vec2d vector<vec1d > void print(char* s, vec2d& pos, int head, int tail) {
if (head > tail) return;
if (head == tail) {
if (s[head] == '(' || s[head] == ')')
printf("()");
else printf("[]");
}
else if (pos[head][tail] == -) {
printf("%c", s[head]);
print(s, pos, head + , tail - );
printf("%c", s[tail]);
}
else {
print(s, pos, head, pos[head][tail]);
print(s, pos, pos[head][tail] + , tail);
}
} void solve(char* s, vec2d& cnt, vec2d& pos) {
int n = strlen(s);
for (int i = ; i < n; i++)
cnt[i][i] = ;
for (int l = ; l < n; l++) {
for (int i = ; i < n - l; i++) {
int j = i + l;
cnt[i][j] = INT_MAX;
if ((s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']')) {
cnt[i][j] = cnt[i + ][j - ];
pos[i][j] = -;
}
for (int k = i; k < j; k++) {
if (cnt[i][k] + cnt[k + ][j] < cnt[i][j]) {
cnt[i][j] = cnt[i][k] + cnt[k + ][j];
pos[i][j] = k;
}
}
}
}
print(s, pos, , n - );
printf("\n");
} int main(void) {
char s[];
while (gets(s)) {
int n = strlen(s);
vec2d cnt(n, vec1d(n, ));
vec2d pos(n, vec1d(n));
solve(s, cnt, pos);
}
return ;
}
[POJ] Brackets Sequence的更多相关文章
- [poj P1141] Brackets Sequence
[poj P1141] Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Special Judge Description ...
- 区间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: 27793 Accepted: 788 ...
- POJ 1141 Brackets Sequence
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29502 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 ...
- POJ1141 Brackets Sequence
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...
随机推荐
- 建立第一个wcf程序
使用管理员权限启动vs (否者将导致ServiceHost开启失败 权限不足) 1.创建一个空的控制台程序 2.添加程序集引用 System.ServiceModel 3.写入一些代码 如下 usin ...
- Quartz.NET 实现定时任务调度
Quartz.NET Quick Start Guide Welcome to the Quick Start Guide for Quartz.NET. As you read this guide ...
- 使用 ConfigurationSection 创建自定义配置节
我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来 ...
- 关于Unity实现游戏录制功能的思考
录制无非两种做法,录制操作和录制行为. 录制操作要考虑到随机行为,但其实也可以两者混合.如果随机行为过多,并且随机行为无法用种子复现,可以完全用录制的方式 最后再统一压缩 这里yy的就是录制行为的做法 ...
- 【大话QT之十三】系统软件自己主动部署实现方案
本篇文章是对[大话QT之十二]基于CTK Plugin Framework的插件版本号动态升级文章的补充,在上篇文章中我们阐述的重点是新版本号的插件已经下载到plugins文件夹后应该怎样更新本地正在 ...
- 一个简单题,引发的思索 + nyoj 1189
题目描述:第一行:给你两个数m和n,m表示有m个数,然后下一行输入m个数,每个数只能选择一次,统计共有多少种情况使得所选数的和大于等于n: 解决本题我想到了两种方法,(题目自己想的,先不考虑超时),第 ...
- 转载:ffmpeg 音视频合成分割
http://blog.csdn.net/jixiuffff/article/details/5709976 当然先安装了 gentoo 下一条命令搞定 emerge ffmpeg 格式转换 (将f ...
- 微信小程序2 - 扩展Page参数
官方默认的Page初始代码为 var option = { /** * 页面的初始数据 */ data: { }, /** * 生命周期函数--监听页面加载 * */ onLoad: function ...
- redis info命令中各个参数的含义
Redis 性能调优相关笔记 2016年09月25日 15:42:04 WenCoding 阅读数:4844更多 个人分类: Redis数据库 info可以使用info [类别]输出指定类别内容i ...
- Struts2漏洞
近日,Struts2曝出2个高危安全漏洞,一个是使用缩写的导航参数前缀时的远程代码执行漏洞,另一个是使用缩写的重定向参数前缀时的开放式重定向漏洞.这些漏洞可使黑客取得网站服务器的“最高权限”,从而使企 ...