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 }
时间: 2024-10-17 07:59:48

Brackets Sequence 括号DP的相关文章

POJ1141 Brackets Sequence (dp动态规划,递归)

本文出自:http://blog.csdn.net/svitter 原题:http://poj.org/problem?id=1141 题意:输出添加括号最少,并且使其匹配的串. 题解: dp [ i ] [ j ] 表示添加括号的个数, pos[ i][ j ] 表示 i , j 中哪个位置分开,使得两部分分别匹配. pos [ i ][ j ] 为-1的时候,说明i, j 括号匹配. 初始值置dp [ i ] [ i ]  = 1; 如果只有一个括号,那么匹配结果必然是差1. 首先判断括号是

POJ 1141 Brackets Sequence (区间dp 记录路径)

题目大意: 给出一种不合法的括号序列,要求构造出一种合法的序列,使得填充的括号最少. 思路分析: 如果只要求输出最少的匹配括号的数量,那么就是简单的区间dp dp[i][j]表示 i - j 之间已经合法了最少添加的括号数. 转移 就是 dp[i] [j] = min  (dp[i+1][j]+1 , dp[ i+ 1] [ k -1 ] + dp[k+1] [j] (i k 位置的括号匹配)) 其次我们要记录路径,你发现  如果 dp [i] [j] 是由 dp [i+1] [j] 转移过来的

ZOJ1463:Brackets Sequence(区间DP)

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. F

POJ 1141 Brackets Sequence (线性dp 括号匹配 经典题)

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

uva1626 poj 1141 Brackets Sequence 区间dp 打印路径

// poj 1141 Brackets Sequence // 也是在紫书上看的一题,uva就是多了一个t组数据. // 经典区间dp // dp(i,j)表示区间[i,j]内所需要增加的括号数目 // 则分为两种情况 // 一种是s[i]和s[j]是匹配的则 // dp[i][j] = min(dp[i][j],dp[i+1][j-1]) // 另外一种情况是不匹配 // dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]){i<k<j}; // 但是无

poj 1141 Brackets Sequence 区间dp,分块记录

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

poj 1141 Brackets Sequence (区间DP)

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

ZOJ1463:Brackets Sequence(间隙DP)

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. F

UVA1626 / ZOJ1463 Brackets sequence 区间DP

简单区间DP (有空串... ...) Brackets sequence Time Limit: 4500MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Let us define a regular brackets sequence in the following way: Empty sequence is a regular sequence. If S is a