码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1141 Brackets Sequence

时间:2016-06-09 23:33:48      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

 

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

 

正解:DP

解题报告:

  DP题,乍一看我居然不会做,也是醉了。开始想用贪心水过,发现会gi烂。

  详细博客:http://blog.csdn.net/lijiecsu/article/details/7589877

  不详细说了,见代码:

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<algorithm>
 7 #include<vector>
 8 using namespace std;
 9 const int MAXN = 1011;
10 char ch[MAXN];
11 int l;
12 int f[MAXN][MAXN],c[MAXN][MAXN];
13 
14 inline void output(int l,int r){
15     if(l>r) return ;
16     if(l==r) {
17     if(ch[l]==( || ch[l]==)) printf("()");
18     else printf("[]");
19     }
20     else{
21     if(c[l][r]>=0) {
22         output(l,c[l][r]);
23         output(c[l][r]+1,r);
24     }
25     else{
26         if(ch[l]==() {
27         printf("(");
28         output(l+1,r-1);
29         printf(")");
30         }
31         else{
32         printf("[");
33         output(l+1,r-1);
34         printf("]");
35         }
36     }
37     }
38 }
39 
40 inline void solve(){
41     scanf("%s",ch);
42     int len=strlen(ch);
43     for(int i=0;i<len;i++) f[i][i]=1;
44     for(int i=0;i<len;i++) for(int j=0;j<len;j++) c[i][j]=-1;
45     for(int l=1;l<=len-1;l++) 
46     for(int i=0;i+l<=len-1;i++){
47         int j=i+l;
48         int minl=f[i][i]+f[i+1][j];
49         c[i][j]=i;
50         for(int k=i+1;k<j;k++){
51         if(minl>f[i][k]+f[k+1][j]) {
52             minl=f[i][k]+f[k+1][j];
53             c[i][j]=k;
54         }
55         }
56         f[i][j]=minl;
57 
58         if(( ch[i]==( && ch[j]==) ) || ( ch[i]==[ && ch[j]==] )) {
59         if(f[i][j]>f[i+1][j-1]) {
60             f[i][j]=f[i+1][j-1];
61             c[i][j]=-1;
62         }
63         }
64     }
65 
66     output(0,len-1);
67     printf("\n");
68 }
69 
70 int main()
71 {
72     solve();
73     return 0;
74 }

 

POJ1141 Brackets Sequence

标签:

原文地址:http://www.cnblogs.com/ljh2000-jump/p/5572837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!