• poj 1141 Brackets Sequence


    Brackets Sequence

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 33911   Accepted: 9816   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

    ()[()]

    题意:

    合法的括号:
    1)空序列是一个合法的序列。
    2)如果S是一个合法的序列,那么(S)和[S]都是合法的序列。
    3)如果A和B是合法的序列,那么AB是合法的序列。
    例如,下面给出的序列是合法的括号序列:
    (),[],(()),([]),()[],()[()],
    不合法的:
    (,[,),)(,([)],([(]
    输出一行,给出包含了给出序列作为子序列的具有最小可能长度的合法括号序列。

    思路:
    设阶段r为子序列的长度,(1<=r<=n-1),状态i为当前子序列的首指针,j=r+i为尾指针,若子序列长度为一的时候,dp[i,i]=1;如果r大于1
    的时候,若(s(i)='['&&s(j)==']')||(s(i)=='('&&s(j)==')')那s(i)到s(j)需要添加的最少字符数取决于s(i+1)到s(j-1),即:
    dp[i,j]=dp[i+1,j-1];否则就需要找到一个过渡的中间变量,即dp[i,j]=min(dp[i,k],dp[k=1,j]);

    代码:
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #define  N 100
    #define INF 0x3f3f3f3f
    using namespace std;
    char str[N];  //初始串
    int dp[N][N]; //状态转移方程组
    int path[N][N]; //记录路径
    void print(int i,int j) //递归输出路径
    {
        if(i>j)
            return ;   //无效位置反回
        if(i==j)       //若子序列含一个字符,则对单个括号输出匹配括号对
        {
            if(str[i]=='['||str[i]==']')
                printf("[]");
            else
                printf("()");
        }
        else if(path[i][j]==-1)
        {
            printf("%c",str[i]);
            print(i+1,j-1);
            printf("%c",str[j]);
        }
        else
        {
            print(i,path[i][j]);
            print(path[i][j]+1,j);
        }
    }
    
    int main()
    {
        while(gets(str))
        {
            int n=strlen(str);
            if(n==0)//空行跳过
            {
                printf("
    ");
                continue;
            }
            memset(dp,0,sizeof(dp));
            memset(path,0,sizeof(path));
            for(int  i=0; i<n; i++)
                dp[i][i]=1;
            for(int d=1; d<n; d++)//枚举子序列长度
            {
                for(int i=0; i<n-d; i++)//枚举子序列首位置
                {
                    int j=i+d;//子序列的尾部
                    dp[i][j]=INF;  
                    if((str[i]=='('&&str[j]==')')||(str[i]=='['&&str[j]==']'))
                    {
                        dp[i][j]=dp[i+1][j-1];
                        path[i][j]=-1;
    
    
                    }
                    for(int k=i; k<j; k++)//枚举中间指针
                    {
                        if(dp[i][j]>dp[i][k]+dp[k+1][j])
                        {
                            dp[i][j]=dp[i][k]+dp[k+1][j];
                            path[i][j]=k; //记录路径
                        }
                    }
                }
            }
            print(0,n-1);
            printf("
    ");
        }
        return 0;
    }
    
    
    


  • 相关阅读:
    make -j 8参数的作用
    使用请求头认证来测试需要授权的 API 接口
    查看Linux系统的平均负载
    服务器负载均衡的基本功能和实现原理
    Oracle RAC学习笔记:基本概念及入门
    详解物化视图(汇总比较有用的资料)
    程序优化注意的一些点
    PR 审批界面增加显示项方法
    Most Common Solutions to FRM-41839 and .tmp Files Not Being Deleted
    APPCORE Routine APIs
  • 原文地址:https://www.cnblogs.com/by-DSL/p/8983693.html
Copyright © 2020-2023  润新知