• 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

     
    解题:这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 }
    View Code
  • 相关阅读:
    ubuntu(14.4) 安装phpmyadmin
    ubuntu(14.04) 安装ssh,并使用root用户登录
    ubuntu(14.04版本) 配置虚拟环境(一个ip对应多个域名)
    ubuntu 中数据的迁移
    ubuntu修改固定ip
    作业调度框架_Quartz
    tomcat设置端口号和默认webapp
    HTTP深入浅出 http请求
    HTTP协议详解
    如何准备阿里社招面试,顺谈 Java 程序员学习中各阶段的建议【转】
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3915713.html
Copyright © 2020-2023  润新知