[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; 当 ...
随机推荐
- maven 动态版本 aliyun阿里云Maven仓库地址——加速你的maven构建
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 转:BOOTSTRAP 增加、关闭、折叠TAB代码下载
http://git.oschina.net/hbbcs/bootStrap-addTabs
- MySQL之desc查看表结构的详细信息
在mysql中如果想要查看表的定义的话:有如下方式可供选择 1.show create table 语句: show create table table_name; 2.desc table_nam ...
- atom搭建markdown环境及问题
1. 搭建markdown环境 > 禁用atom自带的markdown-preview插件(功能简单) > 安装插件:markdown-preview-plus@2.4.16(在markd ...
- Smart Client技术简要总结
摘要:Smart Client简称智能客户端,是Microsoft推出的一种将B/S(瘦客户端)和C/S(胖客户端)结合在一起的一种技术.Smart Client结合了B/S和C/S的优势,具有新的特 ...
- mysql操作索引的sql语句
创建索引 一:唯一索引alter table table_name add unique index_name(column_list); 例如:alter table users_game_task ...
- [elk]logstash的grok匹配逻辑grok+date+mutate
重点参考: http://blog.csdn.net/qq1032355091/article/details/52953837 logstash的精髓: grok插件原理 date插件原理 kv插件 ...
- C#元祖Tuple的事例
数组合并了同样类型的对象.而元祖合并了不同类型的对象.元祖起源于函数编程语言(F#) NET Framework定义了8个泛型Tuple(自NET4.0)和一个静态的Tuple类,他们作用元祖的工厂, ...
- 使用Nginx Lua实现redis高性能http接口
使用Nginx Lua实现redis高性能http接口 时间 -- :: 峰云就她了 原文 http://xiaorui.cc/2015/01/27/使用nginx-lua实现redis高性能http ...
- java中的锁池和等待池
在java中,每个对象都有两个池,锁(monitor)池和等待池 wait() ,notifyAll(),notify() 三个方法都是Object类中的方法. 锁池:假设线程A已经拥有了某个对象(注 ...