This problem can be solved elegantly using dynamic programming.

We maintain two arrays:

  1. cnt[i][j] --- number of parentheses needed to add within s[i..j] inclusively;
  2. pos[i][j] --- position to add the parenthesis within s[i..j] inclusively.

Then there are three cases:

  1. cnt[i][i] = 1;
  2. If s[i] == s[j], cnt[i][j] = cnt[i + 1][j - 1], pos[i][j] = -1 (no need to add any parenthesis);
  3. 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的更多相关文章

  1. [poj P1141] Brackets Sequence

    [poj P1141] Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K   Special Judge Description ...

  2. 区间DP POJ 1141 Brackets Sequence

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

  3. POJ 题目1141 Brackets Sequence(区间DP记录路径)

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 788 ...

  4. POJ 1141 Brackets Sequence

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

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

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

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

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

  7. POJ 1141 Brackets Sequence (区间DP)

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

  8. POJ1141 Brackets Sequence

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

  9. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

随机推荐

  1. 欧洲顶级音频播放软件AIMP

    http://zmingcx.com/europes-top-software-aimp-audio-player.html 音频播放软件众多,耳熟能详的Winamp.Foobar2000.千千静听. ...

  2. wps标准格式

  3. JS经验库

    1.IE状态栏无法结束的问题 function clearStatusBar(){ this.bd = document.body; this.tmp = document.creatElement( ...

  4. 1 bootstrap table null默认显示为 - 要查源码 2 记一个很无语的bug

    本来返回的json 3个true 7个false的 结果显示10个true 因为本来是好的 结果判断的问题 给全部赋值true了

  5. 所谓 A/B test

    A/B测试就是上两个方案,部署后看效果.根据效果和一些结果参数决定采用哪个方案.灰度发布是切一部分业务使用新方案,看效果如何,是否有bug,会遇到什么问题.如果一切OK,就把全部业务切到新的方案上执行 ...

  6. @@identity、scope_identity()、IDENT_CURRENT('tablename')函数的区别

    @@IDENTITY 和SCOPE_IDENTITY 返回在当前会话中的任何表内所生成的最后一个标识值.但是,SCOPE_IDENTITY 只返回插入到当前作用域中的值:@@IDENTITY 不受限于 ...

  7. ubuntu下ssh设置firefox用的反向代理

    mark一下: ssh -D 127.0.0.1:8080 -l root MyIp

  8. Sublime Text的列模式

    Sublime Text的列模式如何操作? 听语音 | 浏览:6551 | 更新:2014-12-09 13:27 | 标签:软件 Sublime Text的列模式如何操作?各个系统不一样,请跟进系统 ...

  9. android-退出动画无效

    在调用 overridePendingTransition(R.anim.anim_scale_in, R.anim.anim_scale_out); 方法设置某个Activity进入和退出动画的时候 ...

  10. 使用 Bolt 实现 GridView 表格控件

    用 Bolt 实现了一个表格控件: 1. 提供 Insert,Remove,Get,Set 接口,可以为表格增删数据: 2. 通过  ItemClass, ItemSetDataFunc 属性来指定显 ...