Brackets Sequence 括号DP

本文介绍了一种算法,用于解决给定括号字符串中加入最少数量的括号以使其匹配的问题。通过逐步分析子区间,算法最终生成完整的匹配括号字符串。

                  Brackets Sequence

题目抽象:给你一个括号字符串,要求你加入最好的括号使得括号匹配。输入匹配后的括号字符串。

分析:由已知的子区间推出大的区间。 cnt[i][j]表示原字符串[i,j]中至少需要加入的括号数。  ans[i][j]存储原字符串[i,j]匹配后的字符串。

特别注意输入的是空字符串。空字符串是匹配的字符串。

 1 import java.util.*;
 2 import java.io.*;
 3 import java.math.*;
 4 
 5 public class Main
 6 {
 7     static Scanner cin = new Scanner(new BufferedInputStream(System.in));
 8     public final static int MS= 105;
 9     static int[][] cnt = new int[MS][MS];
10     static String[][] ans = new String[MS][MS];
11     
12     
13     public static void main(String[] args)
14     {
15         String  str =  cin.nextLine();
16         str.replaceAll(" ","");
17         if(str.length() == 0)
18         {
19             System.out.println();
20             System.exit(0);
21         }
22         char[] ch = str.toCharArray();
23         int len = ch.length;
24         
25         for(int i = 0 ;i<len;i++)
26             for(int j = i;j< len;j++)
27                 cnt[i][j] = Integer.MAX_VALUE;
28         for(int i = 0;i<len;i++)
29             Arrays.fill(ans[i], "");
30         
31         for(int i = len -1;i>=0;i--)
32         {
33             for(int j = i;j<len;j++)
34             {
35                 if(i == j)
36                 {
37                     cnt[i][j] = 1;
38                     if(ch[i] == '('  || ch[i] == ')')
39                         ans[i][i] = "()";
40                     else
41                         ans[i][i] = "[]";
42                 }
43                 else
44                 {
45                     if(ch[i] == '(' && ch[j] == ')')
46                     {
47                         if(cnt[i+1][j-1] < cnt[i][j])
48                         {
49                             cnt[i][j] = cnt[i+1][j-1];
50                             ans[i][j] = "(" + ans[i+1][j-1] + ")";
51                             
52                         }
53                     }
54                     else if(ch[i] == '[' && ch[j] == ']')
55                     {
56                         if(cnt[i+1][j-1] < cnt[i][j])
57                         {
58                             cnt[i][j] = cnt[i+1][j-1];
59                             ans[i][j] = "[" + ans[i+1][j-1] + "]";
60                         }
61                     }
62                     //   求  cnt[i][j]的最小值。
63                     for(int k = i;k<j;k++)
64                     {
65                         if(cnt[i][k] + cnt[k+1][j] < cnt[i][j])
66                         {
67                             cnt[i][j] = cnt[i][k] + cnt[k+1][j];
68                             ans[i][j] = ans[i][k] + ans[k+1][j];
69                         }
70                     }
71                 }
72             }
73         }
74         System.out.println(ans[0][len-1]);
75     }
76 }

 

转载于:https://www.cnblogs.com/hutaishi/p/4665541.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值