BNUOJ 1260 Brackets Sequence

Brackets Sequence

Time Limit: 1000ms

Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 1141
64-bit integer IO format: %lld      Java class name: Main

Special Judge

Let us define a regular brackets sequence in the following way:

1. Empty sequence is a regular sequence. 
2. If S is a regular sequence, then (S) and [S] are both regular sequences. 
3. If A and B are regular sequences, then AB is a regular sequence.

For example, all of the following sequences of characters are regular brackets sequences:

(), [], (()), ([]), ()[], ()[()]

And all of the following character sequences are not:

(, [, ), )(, ([)], ([(]

Some sequence of characters ‘(‘, ‘)‘, ‘[‘, and ‘]‘ is given. You are to find the shortest possible regular brackets sequence, that contains the given character sequence as a subsequence. Here, a string a1 a2 ... an is called a subsequence of the string b1 b2 ... bm, if there exist such indices 1 = i1 < i2 < ... < in = m, that aj = bij for all 1 = j = n.

Input

The input file contains at most 100 brackets (characters ‘(‘, ‘)‘, ‘[‘ and ‘]‘) that are situated on a single line without any other characters among them.

Output

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

([(]

Sample Output

()[()]

Source

Northeastern Europe 2001

解题:这dp啊,我这种学渣啊,每做一次,就有一次新的感觉!水好深啊!

注意是单样例的!改成多样例,立马WA了

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 110;
18 int dp[maxn][maxn],c[maxn][maxn] = {-1};
19 char str[maxn];
20 void print(int i,int j) {
21     if(i > j) return;
22     if(i == j) {
23         if(str[i] == ‘(‘ || str[j] == ‘)‘)
24             printf("()");
25         else printf("[]");
26     } else {
27         if(c[i][j] >= 0) {
28             print(i,c[i][j]);
29             print(c[i][j]+1,j);
30         } else {
31             if(str[i] == ‘(‘) {
32                 printf("(");
33                 print(i+1,j-1);
34                 printf(")");
35             } else {
36                 printf("[");
37                 print(i+1,j-1);
38                 printf("]");
39             }
40         }
41     }
42 }
43 void go() {
44     int len = strlen(str),i,j,k,theMin,t;
45     for(i = 0; i < len; i++) dp[i][i] = 1;
46     for(k = 1; k < len; k++) {
47         for(i = 0; i+k < len; i++) {
48             j = i+k;
49             theMin = dp[i][i]+dp[i+1][j];
50             c[i][j] = i;
51             for(t = i+1; t < j; t++) {
52                 if(dp[i][t]+dp[t+1][j] < theMin) {
53                     theMin = dp[i][t]+dp[t+1][j];
54                     c[i][j] = t;
55                 }
56             }
57             dp[i][j] = theMin;
58             if(str[i] == ‘(‘ && str[j] == ‘)‘ || str[i] == ‘[‘ && str[j] == ‘]‘) {
59                 if(dp[i+1][j-1] < theMin) {
60                     dp[i][j] = dp[i+1][j-1];
61                     c[i][j] = -1;
62                 }
63             }
64         }
65     }
66     print(0,len-1);
67 }
68 int main() {
69     scanf("%s",str);
70     go();
71     puts("");
72     return 0;
73 }

BNUOJ 1260 Brackets Sequence,布布扣,bubuko.com

时间: 2024-08-04 10:06:16

BNUOJ 1260 Brackets Sequence的相关文章

POJ1141 Brackets Sequence

Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regular

poj 1141 Brackets Sequence (区间DP)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25893   Accepted: 7295   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

【POJ 1141】Brackets Sequence

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27996   Accepted: 7936   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

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

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 7885   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

uva1626 poj 1141 Brackets Sequence 区间dp 打印路径

// poj 1141 Brackets Sequence // 也是在紫书上看的一题,uva就是多了一个t组数据. // 经典区间dp // dp(i,j)表示区间[i,j]内所需要增加的括号数目 // 则分为两种情况 // 一种是s[i]和s[j]是匹配的则 // dp[i][j] = min(dp[i][j],dp[i+1][j-1]) // 另外一种情况是不匹配 // dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]){i<k<j}; // 但是无

Brackets sequence

Description: Let us define a regular brackets sequence in the following way: 1.Empty sequence is a regular sequence. 2.If S is a regular sequence, then (S) and [S] are both regular sequences. 3.If A and B are regular sequences, then AB is a regular s

区间DP [POJ 1141] Brackets Sequence

Brackets Sequence Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, th

POJ 1141 Brackets Sequence (区间DP)

Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regular

ZOJ1463:Brackets Sequence(间隙DP)

Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regular sequence. F