• 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 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("
    ");
    68 }
    69 
    70 int main()
    71 {
    72     solve();
    73     return 0;
    74 }
  • 相关阅读:
    git本地仓库基本使用(Repository)
    归途
    PowerDesigner逆向工程导入MYSQL数据库总结(转载)
    协程
    os.path.split 和 os.path.splitext
    转载)Linux平台下安装 python 模块包
    Java之map使用方法
    Java学习(JDBC java连接数据库)
    python -- 找出列表list中的重复元素
    JAVA打印变量类型
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5572837.html
Copyright © 2020-2023  润新知