poj 1141

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

()[()]

黑书第一道DP题


求增加括号数最小的序列;


这题其实看起来蛮眼熟的,它的DP的思想感觉和学长之前讲过的分石子有点像。。。

将序列中的任意子序列看做是一种状态;

比如说子序列:i~j;

考虑转移1:那么如果第i个括号和第j个括号匹配,那么就可以直接考虑子序列i+1~j-1的匹配情况;

考虑转移2:i~j中的任意一个值k,最小的匹配数=(i~k 的最小值 + k+1~j 的最小值)考虑k取全部值时,最小匹配的数;

这样,考虑两种状态转移,即可得到i~j的最小值。


状态转移方程:

dp[i][j]=min(dp[i][k]+dp[k+1][j])

如果第一种情况成立,那么还要考虑第一种情况的作用。


这样可以求出每种状态的最小增加数;


然后还要输出增加后的情况;我发现这种情况我最不擅长了。。。

每次都考虑得很复杂,这次也一样,最后看了看人家的解决方法。。。原来这么简单


用一个数组p[i][j]表示 i~j 的子序列中,最优的分割情况,如果最优的情况是转移1,即除去外层的两个字符,那么p值为-1

这样,递归的调用输出函数,从print(0~n-1) 开始,① 如果p==-1时,输出 i 的值,递归调用print(i+1,j-1),然后 输出j ;

② 当i==j 时,则输出与i位置匹配的成对的括号;

③ 以上都不成立时,则递归调用 print(i, p[i][j]); print(p[i][j]+1,j)

最后,输出的就是这个最短的匹配序列。

ps. 这道的输入啊,真心坑人。。。 wa了很多次,改了不知道多少,最后实在不知道哪里错了,看discuss才知道用吧while(scanf("%s",a)!=EOF) 去掉。。。

结果,一改就A了 (⊙o⊙)…


代码:


#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define MAXN 1000000000
int dp[205][205];
int p[205][205];
char a[205];
int print(int start,int end){
   // cout<<start<<" "<<end<<" "<<dp[start][end]<<" "<<p[start][end]<<endl; 
    if(start>end)
           return 0;
    /*if(dp[start][end]==0){
           for(int i=start;i<=end;i++){
                   printf("%c",a[i]);        
           }
           return 0;
    }*/
    if(start==end){
           if((a[start]=='(')||(a[start]==')'))
                   printf("%s","()");
           else if((a[start]=='[')||(a[start]==']'))   
                   printf("%s","[]");
           return 0;            
    }
    if(p[start][end]==-1){
         printf("%c",a[start]);
         print(start+1,end-1);
         printf("%c",a[end]);
         return 0;
    }
  //  cout<<start<<" "<<end<<" "<<p[start][end]<<endl;
    print(start,p[start][end]);
    print(p[start][end]+1,end);
    return 0;
    
}
int main(){
   // char a[100];
            scanf("%s",a);
            int len=strlen(a);
            for(int i=0;i<len;i++){
                    for(int j=0;j<len;j++){
                            dp[i][j]=MAXN;
                    }        
            }
            memset(p,0,sizeof(p));
            for(int i=0;i<len;i++)
                    dp[i][i]=1;
            for(int i=1;i<len;i++){
                    for(int j=0;j<len-i;j++){
                            int k=j+i;
                            if(((a[j]=='(')&&(a[k]==')'))||((a[j]=='[')&&(a[k]==']'))){
                                 p[j][k]=-1;
                                 if(j==k-1)  dp[j][k]=0;
                                 else dp[j][k]=min(dp[j][k],dp[j+1][k-1]);
                            } 
                            for(int g=j;g<k;g++){
                                 if(dp[j][g]+dp[g+1][k]<dp[j][k]){
                                         dp[j][k]=dp[j][g]+dp[g+1][k];
                                         p[j][k]=g;  
                                 }        
                            }       
                    }        
            }
         //   cout<<dp[0][len-1]<<endl;
            print(0,len-1);
            printf("\n"); 
            memset(a,0,sizeof(a));                        
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值