[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.

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.

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 
 8 #define INT_MAX 0x7fffffff
 9 #define vec1d vector<int>
10 #define vec2d vector<vec1d >
11 
12 void print(char* s, vec2d& pos, int head, int tail) {
13     if (head > tail) return;
14     if (head == tail) {
15         if (s[head] == '(' || s[head] == ')')
16             printf("()");
17         else printf("[]");
18     }
19     else if (pos[head][tail] == -1) {
20         printf("%c", s[head]);
21         print(s, pos, head + 1, tail - 1);
22         printf("%c", s[tail]);
23     }
24     else {
25         print(s, pos, head, pos[head][tail]);
26         print(s, pos, pos[head][tail] + 1, tail);
27     }
28 }
29 
30 void solve(char* s, vec2d& cnt, vec2d& pos) {
31     int n = strlen(s);
32     for (int i = 0; i < n; i++)
33         cnt[i][i] = 1;
34     for (int l = 1; l < n; l++) {
35         for (int i = 0; i < n - l; i++) {
36             int j = i + l;
37             cnt[i][j] = INT_MAX;
38             if ((s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']')) {
39                 cnt[i][j] = cnt[i + 1][j - 1];
40                 pos[i][j] = -1;
41             }
42             for (int k = i; k < j; k++) {
43                 if (cnt[i][k] + cnt[k + 1][j] < cnt[i][j]) {
44                     cnt[i][j] = cnt[i][k] + cnt[k + 1][j];
45                     pos[i][j] = k;
46                 }
47             }
48         }
49     }
50     print(s, pos, 0, n - 1);
51     printf("\n");
52 }
53 
54 int main(void) {
55     char s[110];
56     while (gets(s)) {
57         int n = strlen(s);
58         vec2d cnt(n, vec1d(n, 0));
59         vec2d pos(n, vec1d(n));
60         solve(s, cnt, pos);
61     }
62     return 0;
63 }

 

目录
相关文章
codeforces 327 B. Hungry Sequence
题目就是让你输出n个数的序列,要保证该序列是递增的,并且第i个数的前面不能保护它的约数,我直接先对前100000的素数打表,然后输出前n个,so easy。
44 0
|
算法
LeetCode 128. Longest Consecutive Sequence
给定一个未排序的整数数组,找出最长连续序列的长度。 要求算法的时间复杂度为 O(n)。
88 0
LeetCode 128. Longest Consecutive Sequence
LeetCode 263. Ugly Number
编写一个程序判断给定的数是否为丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。
93 0
LeetCode 263. Ugly Number
LeetCode 264. Ugly Number II
编写一个程序,找出第 n 个丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。
73 0
LeetCode 264. Ugly Number II
LeetCode 424. Longest Repeating Character Replacem
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次。在执行上述操作后,找到包含重复字母的最长子串的长度。
104 0
LeetCode 424. Longest Repeating Character Replacem
|
人工智能
POJ 2533 Longest Ordered Subsequence
POJ 2533 Longest Ordered Subsequence
107 0
|
C++
[LeetCode] Binary Tree Longest Consecutive Sequence
Problem Description: Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from so...
909 0