[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; 当 ...
随机推荐
- 欧洲顶级音频播放软件AIMP
http://zmingcx.com/europes-top-software-aimp-audio-player.html 音频播放软件众多,耳熟能详的Winamp.Foobar2000.千千静听. ...
- wps标准格式
- JS经验库
1.IE状态栏无法结束的问题 function clearStatusBar(){ this.bd = document.body; this.tmp = document.creatElement( ...
- 1 bootstrap table null默认显示为 - 要查源码 2 记一个很无语的bug
本来返回的json 3个true 7个false的 结果显示10个true 因为本来是好的 结果判断的问题 给全部赋值true了
- 所谓 A/B test
A/B测试就是上两个方案,部署后看效果.根据效果和一些结果参数决定采用哪个方案.灰度发布是切一部分业务使用新方案,看效果如何,是否有bug,会遇到什么问题.如果一切OK,就把全部业务切到新的方案上执行 ...
- @@identity、scope_identity()、IDENT_CURRENT('tablename')函数的区别
@@IDENTITY 和SCOPE_IDENTITY 返回在当前会话中的任何表内所生成的最后一个标识值.但是,SCOPE_IDENTITY 只返回插入到当前作用域中的值:@@IDENTITY 不受限于 ...
- ubuntu下ssh设置firefox用的反向代理
mark一下: ssh -D 127.0.0.1:8080 -l root MyIp
- Sublime Text的列模式
Sublime Text的列模式如何操作? 听语音 | 浏览:6551 | 更新:2014-12-09 13:27 | 标签:软件 Sublime Text的列模式如何操作?各个系统不一样,请跟进系统 ...
- android-退出动画无效
在调用 overridePendingTransition(R.anim.anim_scale_in, R.anim.anim_scale_out); 方法设置某个Activity进入和退出动画的时候 ...
- 使用 Bolt 实现 GridView 表格控件
用 Bolt 实现了一个表格控件: 1. 提供 Insert,Remove,Get,Set 接口,可以为表格增删数据: 2. 通过 ItemClass, ItemSetDataFunc 属性来指定显 ...