UVA1626-Brackets sequence(动态规划基础)

Posted npugen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA1626-Brackets sequence(动态规划基础)相关的知识,希望对你有一定的参考价值。

Problem UVA1626-Brackets sequence

Time Limit: 4500 mSec

技术分享图片 Problem Description

技术分享图片

 

 

 

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs. The input file contains at most 100 brackets (characters ‘(’, ‘)’, ‘[’ and ‘]’) that are situated on a single line without any other characters among them.

 

技术分享图片 Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. Write to the output file a single line that contains some regular brackets sequence that has the minimal possible length and contains the given sequence as a subsequence.
 

技术分享图片 Sample Input

1

([(]
 

技术分享图片 Sample Output

()[()]

 

题解:这个题挺好的,区间dp,可以写成记忆化搜索,容易忽略的地方是如果区间两边的括号匹配,那么可以用中间的部分转移,然后就是普通的分成左区间和右区间进行转移,这个题比较有价值的地方在于打印解的过程,应该学习一下,就是根据结果,逆推回去,这个方便在不用中间记录转移路径,代价就是时间上会有额外的开销,不过一般不至于因此就TLE,因为解一般很少。输入输出有坑,需要用fgets,并且注意fgets会把最后的‘ ‘读进来,因此真实串的长度需要-1.

 

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 const int maxn = 100 + 10;
 6 
 7 int iCase, dp[maxn][maxn];
 8 char bra[maxn];
 9 bool vis[maxn][maxn];
10 
11 bool match(char a, char b) {
12     if ((a == ( && b == )) || (a == [ && b == ])) return true;
13     return false;
14 }
15 
16 int DP(int l, int r) {
17     if (dp[l][r] >= 0) return dp[l][r];
18     if (l == r) return dp[l][r] = 1;
19     if (l > r) return  dp[l][r] = 0;
20 
21     int &ans = dp[l][r];
22     ans = r - l + 1;
23     if (match(bra[l], bra[r])) {
24         ans = min(ans, DP(l + 1, r - 1));
25     }
26     for (int k = l; k < r; k++) {
27         ans = min(ans, DP(l, k) + DP(k + 1, r));
28     }
29     return ans;
30 }
31 
32 void ans_print(int l, int r) {
33     if (l > r) return;
34     if (l == r) {
35         if (bra[l] == ( || bra[l] == )) {
36             printf("()");
37         }
38         else {
39             printf("[]");
40         }
41         return;
42     }
43 
44     int &ans = dp[l][r];
45     if (match(bra[l], bra[r]) && ans == dp[l + 1][r - 1]) {
46         printf("%c", bra[l]);
47         ans_print(l + 1, r - 1);
48         printf("%c", bra[r]);
49         return;
50     }
51     else {
52         for (int k = l; k < r; k++) {
53             if (ans == dp[l][k] + dp[k + 1][r]) {
54                 ans_print(l, k);
55                 ans_print(k + 1, r);
56                 return;
57             }
58         }
59     }
60 }
61 
62 int main()
63 {
64     //freopen("input.txt", "r", stdin);
65     scanf("%d
", &iCase);
66     while (iCase--) {
67         fgets(bra, maxn, stdin);
68         memset(dp, -1, sizeof(dp));
69         int len = strlen(bra);
70         int ans = DP(0, len - 2);
71         ans_print(0, len - 2);
72         printf("
");
73         if (iCase) printf("
");
74         fgets(bra, maxn, stdin);
75     }
76     return 0;
77 }

 


以上是关于UVA1626-Brackets sequence(动态规划基础)的主要内容,如果未能解决你的问题,请参考以下文章

uva1626 Brackets sequence

1626 - Brackets sequence——[动态规划]

uva 1584.Circular Sequence

UVa 1584 Circular Sequence --- 水题

UVa 10534 Wavio Sequence

UVA - 10706 Number Sequence