区间dp实战练习

题解报告:poj 2955 Brackets(括号匹配)

Description

We give the following inductive definition of a “regular brackets” sequence:

  • the empty sequence is a regular brackets sequence,
  • if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
  • if a and b are regular brackets sequences, then ab is a regular brackets sequence.
  • no other sequence is a regular brackets sequence

For instance, all of the following character sequences are regular brackets sequences:

(), [], (()), ()[], ()[()]

while the following character sequences are not:

(, ], )(, ([)], ([(]

Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1i2, …, im where 1 ≤ i1 < i2 < … < im ≤ nai1ai2 … aim is a regular brackets sequence.

Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].

Input

The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters ()[, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.

Output

For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

Sample Input

((()))
()()()
([]])
)[)(
([][][)
end

Sample Output

6
6
4
0
6
解题思路:经典区间dp,要求找出能配对的括号的最大个数。定义dp[i][j]表示第i到第j个括号的最大匹配数目,那么当第i个括号与第j个括号配对时,dp[i][j]为小的区间最大匹配数加上2即dp[i][j]=dp[i+1][j-1]+2,然后枚举区间[i,j]之间的断点k,合并更新区间[i,j]的最值,最终的答案就是dp[1][length]。时间复杂度为O(n^3)。
AC代码(32ms):
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<string.h>
 5 using namespace std;
 6 const int maxn=105;
 7 char str[maxn];int length,dp[maxn][maxn];
 8 bool check(char ch1,char ch2){
 9     return (ch1=='('&&ch2==')')||(ch1=='['&&ch2==']');
10 }
11 int main(){
12     while(~scanf("%s",str+1)&&strcmp(str+1,"end")){
13         memset(dp,0,sizeof(dp));length=strlen(str+1);
14         for(int len=1;len<=length;++len){//枚举区间长度1~length
15             for(int i=1;i<=length-len;++i){//区间起点i
16                 int j=i+len;//区间终点j
17                 if(check(str[i],str[j]))dp[i][j]=dp[i+1][j-1]+2;
18                 for(int k=i;k<j;++k)//区间最值合并
19                     dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
20             }
21         }
22         printf("%d
",dp[1][length]);
23     }
24     return 0;
25 }

题解报告:NYOJ #15 括号匹配(二)

描述

给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来。
如:
[]是匹配的
([])[]是匹配的
((]是不匹配的
([)]是不匹配的

输入

第一行输入一个正整数N,表示测试数据组数(N<=10)
每组测试数据都只有一行,是一个字符串S,S中只包含以上所说的四种字符,S的长度不超过100

输出

对于每组测试数据都输出一个正整数,表示最少需要添加的括号的数量。每组测试输出占一行

样例输入

4
[]
([])[]
((]
([)]

样例输出

0
0
3
2
解题思路:上一道题的变形,同样求出给定括号的最大匹配数,那么最少还需要添加的括号数为length-dp[1][length]。
AC代码一(16ms):
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<string.h>
 5 using namespace std;
 6 const int maxn=105;
 7 char str[maxn];int t,length,dp[maxn][maxn];
 8 bool check(char ch1,char ch2){
 9     return (ch1=='('&&ch2==')')||(ch1=='['&&ch2==']');
10 }
11 int main(){
12     while(~scanf("%d",&t)){
13         while(t--){
14             scanf("%s",str+1);
15             memset(dp,0,sizeof(dp));length=strlen(str+1);
16             for(int len=1;len<=length;++len){//区间长度
17                 for(int i=1;i<=length-len;++i){//区间起点i
18                     int j=i+len;//区间终点j
19                     if(check(str[i],str[j]))dp[i][j]=dp[i+1][j-1]+2;
20                     for(int k=i;k<j;++k)//更新区间最值
21                         dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
22                 }
23             }
24             printf("%d
",length-dp[1][length]);
25         }
26     }
27     return 0;
28 }

AC代码二(16ms):记忆化搜索。定义dp[i][j]为第i个到第j个括号间至少需要增加的括号数,那么当区间[i,j]为空(i>j)即不含括号时,dp[i][j]=0;当区间只含一个字符即i==j时,dp[i][j]=1,表示至少需要增加1个括号;当第i个括号与第j个括号配对时,dp[i][j]为上一个状态子区间[i+1,j-1]中至少需要增加的括号数即dp[i+1][j-1];然后枚举[i,j]中的断点k,依次合并更新区间[i,j]值dp[i][j],表示从第i个字符到第k个字符至少需要增加的括号数加上从第k+1个字符到第j个字符至少需要增加的括号数,在这所有的k中取最小值作为dp[i][j]即可。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<string.h>
 5 using namespace std;
 6 const int maxn=105;
 7 const int inf=0x7fffffff;
 8 char str[maxn];int t,length,dp[maxn][maxn];
 9 bool check(char ch1,char ch2){
10     return (ch1=='('&&ch2==')')||(ch1=='['&&ch2==']');
11 }
12 int dfs(int x,int y){
13     if(x>y)return 0;
14     else if(dp[x][y]>=0)return dp[x][y];//表示已搜索过了,此时直接返回当前区间[x,y]的值
15     else if(x==y)return dp[x][y]=1;//单个字符时,需要增加的括号数为1
16     else{
17         int var=inf;//先初始化为无穷大
18         if(check(str[x],str[y]))var=dfs(x+1,y-1);//如果能匹配,至少需要增加的括号数为上一个状态的值dp[x+1][y-1]
19         for(int k=x;k<y;++k)
20             var=min(var,dfs(x,k)+dfs(k+1,y));//再更新当前区间[x,y]最少需要增加的括号数,由其子状态得来
21         return dp[x][y]=var;//返回并且赋值
22     }
23 }
24 int main(){
25     while(~scanf("%d",&t)){
26         while(t--){
27             scanf("%s",str+1);memset(dp,-1,sizeof(dp));
28             length=strlen(str+1);
29             printf("%d
",dfs(1,length));
30         }
31     }
32     return 0;
33 }

题解报告:NYOJ #746 整数划分(四)

描述

暑假来了,hrdv 又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近遇到了一个难题,让他百思不得其解,他非常郁闷。。亲爱的你能帮帮他吗?

问题是我们经常见到的整数划分,给出两个整数 n , m ,要求在 n 中加入m - 1 个乘号,将n分成m段,求出这m段的最大乘积

输入

第一行是一个整数T,表示有T组测试数据
接下来T行,每行有两个正整数 n,m ( 1<= n < 10^19, 0 < m <= n的位数);

输出

输出每组测试样例结果为一个整数占一行

样例输入

2
111 2
1111 2

样例输出

11
121
解题思路:定义dp[i][j]表示前i位插入j个乘号(i>j)能得到的最大乘积。根据区间dp思想我们可以从插入较少乘号的结果算出插入较多乘号的结果,即由子问题计算推出大问题。状态转移的关键点是,当插入第j个乘号时,需要枚举其放的位置j~i-1中哪个位置能产生最大的乘积值,则方程可表示为dp[i][j]=max(dp[i][j],dp[k][j-1]*num[k+1][i]),表示前k位中插入j-1(k>j-1)个乘号的最大值乘以从第k+1位到i位组成的数,取所有k中得到的最大乘积值作为dp[i][j],最终的答案就是dp[len][m-1]。
AC代码(4ms):
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<string.h>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn=25; 
 8 int t,len;LL m;char str[maxn];LL dp[maxn][maxn],num[maxn][maxn];
 9 int main(){
10     while(cin>>t){
11         while(t--){
12             cin>>(str+1)>>m;len=strlen(str+1);memset(dp,0,sizeof(dp));memset(num,0,sizeof(num));
13             for(int i=1;i<=len;++i){
14                 num[i][i]=str[i]-'0';//num[i,i]的值为其本身
15                 for(int j=i+1;j<=len;++j)//num[i][j]表示区间[i,j]组成对应的数
16                     num[i][j]=num[i][j-1]*10+(str[j]-'0');
17                 dp[i][0]=num[1][i];//前i位插入0个乘号,其值为1~i对应的数即num[1][i]
18             }
19             for(int j=1;j<m;++j)//插入j个乘号
20                 for(int i=j+1;i<=len;++i)//则最少有j+1位,枚举j+1~len位,此时插入j个乘号
21                     for(int k=j;k<i;++k)//枚举j~i-1中的分断点k,找出最大的乘积作为dp[i][j]的值,即前i位加入j个乘号所能得到的最大乘积
22                         dp[i][j]=max(dp[i][j],dp[k][j-1]*num[k+1][i]);
23             cout<<dp[len][m-1]<<endl;//表示前len位插入m-1个乘号
24         }
25     }
26     return 0;
27 }
原文地址:https://www.cnblogs.com/acgoto/p/9647496.html