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. sql关于group by之后把每一条记录的详情的某个字段值合并提取的方法

    在利用group by写了统计语句之后,还有一个查看每一个记录详情的需求, 首先想到的是根据group by的条件去拼接查询条件, 但是条件有点多,拼接起来不仅麻烦,还容易出错, 所以想到要在grou ...

  2. 由上而下层层剖析细说PCI+ExpresS+11新版精髓

    https://wenku.baidu.com/view/9a16c41fa300a6c30c229f87.html

  3. SpringBoot入门系列:第三篇 日志输出

    http://blog.csdn.net/lxhjh/article/details/51752419

  4. PHP学习笔记(8)验证码使用session对比

    知识点: 1. session获取其他页面的变量: (1)先在画验证码php里开启session_start(),$_SESSION['随便起名']=验证码字符串, (2)再在submit提交到act ...

  5. Unity3D中uGUI事件系统简述及使用方法总结

    Unity3D的uGUI系统的将UI可能触发的事件分为12个类型,即EventTriggerType枚举的12个值.如下图所示: 先以PointerClick为例.这个是用于某点点击事件.其他事件都可 ...

  6. zookeeper安装与集群搭建

    此处以centos系统下zookeeper安装为例,详细步骤可参考官网文档:zookeeper教程 一.单节点部署 1.下载zookeeper wget http://mirrors.hust.edu ...

  7. yum安装的JDK的没有配置环境变量但是在/usr/bin下面都做了软链接

    [root@st152 /usr/bin]# ll |grep javalrwxrwxrwx    1 root root         22 Nov 28 22:14 java -> /et ...

  8. 卧槽! JavaScript JVM运行Java!!

    由于任何计算机语言都具有巨大的灵活性,软件世界变得有点疯狂.一旦你已经吸收了用这种语言编写的编译器的想法,那么它会编译还有什么可以留下来的?但是......用JavaScript编写的Java虚拟机J ...

  9. HTML5课程

    1.新语义化标签:section.header.footer.nav.article.aside.figure.dialog.time.meter.mark.progress.video 2.最新的属 ...

  10. CI cookie 存放数组

      #ci cookie 由于不能存放数组,所有必须序列化之后在存入数组中 #定义数组 $cookie_array=array( 'shop_id'=>$gid, 'shop_name'=> ...