• POJ 1141 Brackets Sequence


    黑书动态规划第一题

                                                Brackets Sequence
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 21666   Accepted: 6068   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 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

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 const int maxn=111;
     8 
     9 int dp[maxn][maxn],path[maxn][maxn];
    10 char str[maxn];
    11 
    12 void print(int i,int j)
    13 {
    14     if(i>j) return ;
    15     else if(i==j)
    16     {
    17         if(str[i]=='['||str[i]==']') printf("[]");
    18         else printf("()");
    19     }
    20     else if(path[i][j]==-1)
    21     {
    22         printf("%c",str[i]);
    23         print(i+1,j-1);
    24         printf("%c",str[j]);
    25     }
    26     else
    27     {
    28         int k=path[i][j];
    29         print(i,k);
    30         print(k+1,j);
    31     }
    32 
    33 }
    34 
    35 int main()
    36 {
    37     while(gets(str))
    38     {
    39         int n=strlen(str);
    40 
    41         if(n==0) { putchar(10);continue; }
    42 
    43         memset(dp,0,sizeof(dp));
    44 
    45         for(int i=0;i<n;i++)  dp[i][i]=1;
    46 
    47         for(int r=1;r<n;r++)
    48         for(int i=0;i<n-r;i++)
    49         {
    50             int j=i+r;
    51             dp[i][j]=999999999;
    52             if((str[i]=='['&&str[j]==']')||(str[i]=='('&&str[j]==')'))
    53                 if(dp[i][j]>dp[i+1][j-1])
    54                     dp[i][j]=dp[i+1][j-1],path[i][j]=-1;
    55 
    56             for(int k=i;k<j;k++)
    57                 if(dp[i][j]>(dp[i][k]+dp[k+1][j]))
    58                 dp[i][j]=dp[i][k]+dp[k+1][j],path[i][j]=k;
    59         }
    60 
    61         print(0,n-1);
    62         putchar(10);
    63     }
    64 
    65     return 0;
    66 }
  • 相关阅读:
    Python find() 方法
    enumerate函数
    【Unity】项目工程源码
    【Unity】UGUI聊天消息气泡 随文本内容自适应
    Unity读Excel 输出PC端(Windows)后不能读取的问题
    【C#】读取Excel中嵌套的Json对象,Json带斜杠的问题(其三)
    【C#】读取Excel中嵌套的Json对象,Json带斜杠的问题(其二)
    【C#】读取Excel中嵌套的Json对象,Json带斜杠的问题(其一)
    Unity输出PC端(Windows) 拖拽文件到app中
    Android Studio报错Error:Failed to open zip file. Gradle's dependency cache may be corrupt
  • 原文地址:https://www.cnblogs.com/CKboss/p/3091067.html
Copyright © 2020-2023  润新知