• POJ 1141 Brackets Sequence(DP)


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

     
     
     
     
    题目描述:
         给出一个由(、)、[、]组成的字符串,添加最少的括号使得所有的括号匹配,输出最后得到的字符串。

    解题思路:
          用DP求最少需要括号数:以p从1到n(字符串长度),记录下从i到i+p需要添加的最少括号数f[i][j],同时记录下中间需要添加括号的位置pos[i][j]——为-1表示不需要添加。
     
     
    DP
     
     
    #include<stdio.h>
    #include<string.h>

    #define MAXN 120
    const int INF=0x7fffffff;

    int f[MAXN][MAXN],pos[MAXN][MAXN];
    char s[MAXN];

    int n;

    int DP()
    {
    n=strlen(s);
    memset(f,0,sizeof(f));

    for(int i=n;i>0;i--)
    {
    s[i]=s[i-1];
    f[i][i]=1;
    }
    int tmp;
    for(int p=1;p<=n;p++)
    {
    for(int i=1;i<=n-p;i++)
    {
    int j=i+p;
    f[i][j]=INF;
    if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
    {
    tmp=f[i+1][j-1];
    if(tmp<f[i][j])
    f[i][j]=tmp;
    }
    pos[i][j]=-1;
    for(int k=i;k<j;k++)
    {
    tmp=f[i][k]+f[k+1][j];
    if(tmp<f[i][j])
    {
    f[i][j]=tmp;
    pos[i][j]=k;
    }
    }
    }
    }
    return f[1][n];
    }

    void print(int beg,int end)
    {
    if(beg>end)return ;
    if(beg==end)
    {
    if(s[beg]=='('||s[beg]==')')
    printf("()");
    else printf("[]");
    }
    else
    {
    if(pos[beg][end]==-1)
    {
    if(s[beg]=='(')
    {
    printf("(");
    print(beg+1,end-1);
    printf(")");
    }
    else
    {
    printf("[");
    print(beg+1,end-1);
    printf("]");
    }
    }
    else
    {
    print(beg,pos[beg][end]);
    print(pos[beg][end]+1,end);
    }
    }
    }
    int main()
    {
    // while(scanf("%s",&s)!=EOF) //这样输入会 WR
    scanf("%s",&s);

    DP();
    print(1,n);
    printf("\n");

    return 0;
    }
  • 相关阅读:
    ORACLE分页SQL语句
    oracle中主键自增
    html5video初试牛刀api
    如何修改tomcat的端口号
    用Java来获取访问者真实的IP地址
    根据txt文件处理的方法总结
    CXF 做 webservice 简单例子demo
    java创建TXT文件并进行读、写、修改操作
    POJ1047 Round and Round We Go
    NYOJ305 表达式求值
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2429438.html
Copyright © 2020-2023  润新知