POJ1141 Brackets Sequence

来源:互联网 发布:聊天表情制作软件 编辑:程序博客网 时间:2024/05/17 22:00
                                                                         Brackets Sequence
 Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10741 Accepted: 2856 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

Northeastern Europe 2001
这是我开始学习DP做的第一个题,想了很久,才明白其中的状态转移的方程。注意两个不同的转移。当a[i]与a[j]匹配时,c[i,j]=c[i+1,j-1].不匹配时,在i->j找出使c[i,k]+c[k+1,j]最小的K,c[i,j]=c[i,k]+c[k+1,j].下面是我的代码,比较粗造。呵呵!
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char a[110];
int c[110][110],b[110][110];
void output(int x,int y)
{
     if(x>y)  return ;
     if(x==y)
     {
         if(a[x]=='('||a[x]==')')
             printf("()");
         else
             printf("[]");
     }
     else
     {
          if(b[x][y]==-1)
          {
              printf("%c",a[x]);
              output(x+1,y-1);
              printf("%c",a[y]);
          }            
          else
          {
              output(x,b[x][y]);
              output(b[x][y]+1,y);
          }
     }
}                 
int main()
{
    while(gets(a))
    {
        int r,k,i,j;
        int g=strlen(a);
        if(!g)
        { printf("/n"); continue;}
        memset(c,0,sizeof(c));
        for(i=0;i<g;i++)  c[i][i]=1;
        for(r=1;r<g;r++)
        {
             for(i=0;i<g-r;i++)
             {
                  j=i+r;
                  c[i][j]=1000000;
                  if((a[i]=='('&&a[j]==')')||(a[i]=='['&&a[j]==']'))                              
                  {
                      if(c[i][j]>c[i+1][j-1])
                         c[i][j]=c[i+1][j-1],b[i][j]=-1;
                  }
                  //else
                  //{
                      for(k=i;k<j;k++)
                      {
                          if(c[i][k]+c[k+1][j]<c[i][j])
                              c[i][j]=c[i][k]+c[k+1][j],b[i][j]=k;
                      }
                  //}
             }
        }
        output(0,g-1);
        printf("/n");
    }
    return 0;
}                                                                              

原创粉丝点击