poj 1141 Brackets Sequence dp



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]表示i,j之间最少需要增加的符号个数。用path记录路径。状态转移dp[i][j]=min(dp[i][k]+dp[k+1][j]);意思是从哪个点分开的话最优。当str[i]==str[j]时dp[i][j]=dp[i+1][j-1];
 
 
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
        
        
#include
        
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
              #include 
              
                #define ll long long #define eps 1e-9 #define pi acos(-1.0) #define INF 0x3f3f3f3f #define M 10000+10 using namespace std; int dp[200][200],path[200][200]; int n; char str[200]; void print(int i,int j) { if(i>j) //前一步出现了已经匹配的状态 return; if(i==j) { if(str[i]==')'||str[i]=='(') printf("()"); else if(str[i]=='['||str[i]==']') printf("[]"); } else if(path[i][j]==-1) { printf("%c",str[i]); print(i+1,j-1); printf("%c",str[j]); } else { int k=path[i][j]; print(i,k); print(k+1,j); } } int main() { int i,j,k; while(gets(str)) { n=strlen(str); if(n==0) //空串也是符合要求的 { printf("\n"); continue; } memset(dp,0,sizeof(dp)); int len; for(len=1;len 
               
                 dp[j+1][k-1]) { dp[j][k]=dp[j+1][k-1]; path[j][k]=-1; //表示不需要另外加括号就已经匹配 } for(i=j;i 
                
                  dp[j][i]+dp[i+1][k]) { dp[j][k]=dp[j][i]+dp[i+1][k]; path[j][k]=i; } } print(0,n-1); printf("\n"); } return 0; } 
                 
                
               
             
            
           
          
        
       
       
      
      
     
     
    
    
还有一种就是自顶向下,记忆化搜索。
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
        
        
#include
        
        
         
         
#include
         
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
               #include 
               
                 #define ll long long #define eps 1e-9 #define pi acos(-1.0) #define INF 0x3f3f3f3f const int M=5e4+10; using namespace std; int dp[200][200],path[200][200],vis[200][200]; int n; char str[200]; int solve(int i,int j) { int k; if(vis[i][j]) return dp[i][j]; int ans=INF; if(i>j) return 0; else if(i==j) return 1; if(str[i]=='('&&str[j]==')'||str[i]=='['&&str[j]==']') { int t1=solve(i+1,j-1); if(ans>t1) { ans=t1; path[i][j]=-1; } } for(k=i;k 
                
                  t1+t2) { ans=t1+t2; path[i][j]=k; } } dp[i][j]=ans; vis[i][j]=1; return ans; } void print(int i,int j) { if(i>j) return; if(i==j) { if(str[i]==')'||str[i]=='(') printf("()"); else if(str[i]=='['||str[i]==']') printf("[]"); } else if(path[i][j]==-1) { printf("%c",str[i]); print(i+1,j-1); printf("%c",str[j]); } else { int k=path[i][j]; print(i,k); print(k+1,j); } } int main() { while(gets(str)) { n=strlen(str); if(n==0) { printf("\n"); continue; } memset(vis,0,sizeof(vis)); solve(0,n-1); print(0,n-1); printf("\n"); } } 
                 
                
              
             
            
           
         
        
        
       
       
      
      
     
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值