POJ 2955 区间DP Brackets

求一个括号的最大匹配数,这个题可以和UVa 1626比较着看。

注意题目背景一样,但是所求不一样。

回到这道题上来,设d(i, j)表示子序列Si ~ Sj的字符串中最大匹配数,如果Si 与 Sj能配对,d(i, j) = d(i+1, j-1)

然后要枚举中间点k,d(i, j) = max{ d(i, k) + d(k+1, j) }

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 const int maxn = 100 + 10;
 8
 9 int n;
10 char s[maxn];
11 int d[maxn][maxn];
12
13 bool inline match(char c1, char c2)
14 {
15     if(c1 == ‘[‘ && c2 == ‘]‘) return true;
16     if(c1 == ‘(‘ && c2 == ‘)‘) return true;
17     return false;
18 }
19
20 int main()
21 {
22     while(scanf("%s", s) == 1 && s[0] != ‘e‘)
23     {
24         n = strlen(s);
25         for(int i = 0; i + 1 < n; i++)
26         {
27             if(match(s[i], s[i+1])) d[i][i+1] = 2;
28             else d[i][i+1] = 0;
29         }
30
31         for(int l = 3; l <= n; l++)
32         {
33             for(int i = 0; i + l - 1 < n; i++)
34             {
35                 int j = i + l - 1;
36                 d[i][j] = 0;
37                 if(match(s[i], s[j])) d[i][j] = d[i+1][j-1] + 2;
38                 for(int k = i; k < j; k++)
39                     d[i][j] = max(d[i][j], d[i][k] + d[k+1][j]);
40             }
41         }
42
43         printf("%d\n", d[0][n-1]);
44     }
45
46     return 0;
47 }

代码君

时间: 2024-10-08 20:04:16

POJ 2955 区间DP Brackets的相关文章

poj 2955 区间dp(括号匹配)

#include<stdio.h> #include<string.h> #include<iostream> using namespace std; int max(int a,int b) { return a>b?a:b; } int main() { char str[110]; int dp[110][110]; int i,j; while(~scanf("%s",str)) { int len=strlen(str); if(s

poj 3280(区间DP)

Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7869   Accepted: 3816 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow a

poj 1141 区间dp

题目链接:http://poj.org/problem?id=1141 题解:略 代码: #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define ll long long const int maxn=1e2+5; const int INF=0x3f3f3f3f; int dp[105][105]; int

poj 1651 区间dp

题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the prod

POJ 1651 区间DP Multiplication Puzzle

此题可以转化为最优矩阵链乘的形式,d(i, j)表示区间[i, j]所能得到的最小权值. 枚举最后一个拿走的数a[k],状态转移方程为d(i, j) = min{ d(i, k) + d(k, j) + a[i] * a[k] * a[j] } 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std;

poj 1651(区间dp)

乱改出真知,做不动了,水平有限,大概了解一下,去做树形dp了,以后回来再学 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn=100+10; int a[maxn]; int dp[maxn][maxn]; int n; const int inf=0x3f3f3f3f; int m

poj 1141 区间dp+递归打印路径

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

poj 3186区间dp

给你一个数列   然后从里面取数  只能从队头或队尾取出   取出的值乘以取出的顺序i(及第几个取出的)  求累加和的最大值: dp[i][j]表示去了i次   从左边去了j个的最大值:  然后地推下去: dp[i][j]=max(dp[i-1][j]*num[j]*i,num[i-1][j]+num[n-i+j+1]*i)注意j为0的情况:再说这道题坑吧     num数组开为局部变量wa   开为int64  还是wa  真是无语了 #include<stdio.h> #include&l

POJ 2955:Brackets(区间DP)

http://poj.org/problem?id=2955 题意:给出一串字符,求括号匹配的数最多是多少. 思路:区间DP. 对于每个枚举的区间边界,如果两边可以配对成括号,那么dp[i][j] = dp[i+1][j-1] + 2,表示由上一个状态加上当前的贡献. 然后和普通的区间合并一样去更新. 1 #include <cstring> 2 #include <cstdio> 3 #include <iostream> 4 #include <string&