POJ 2955 Brackets (区间dp入门)

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 a1a2an, 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 i1, i2, …, im where 1 ≤ i1 < i2 < … < imn, ai1ai2 … 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之间最大的匹配字符数。if ((s[i]==‘(‘&&s[j]==‘)‘)||(s[i]==‘[‘&&s[j]==‘]‘)) ————>dp[i][j]=dp[i+1][j-1]+2; 懂吧代码如下:
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <iostream>
 6
 7 using namespace std;
 8 char s[110];
 9 int dp[110][110];
10 int main()
11 {
12     //freopen("de.txt","r",stdin);
13     while (~scanf("%s",&s))
14     {
15         if (s[0]==‘e‘)
16         break ;
17         memset(dp,0,sizeof dp);
18         int len=strlen(s);
19         for (int k=1;k<len;++k)
20         {
21             for (int i=0,j=k;j<len;++i,++j)
22             {
23                 if ((s[i]==‘(‘&&s[j]==‘)‘)||(s[i]==‘[‘&&s[j]==‘]‘))
24                     dp[i][j]=dp[i+1][j-1]+2;
25                 for (int x=i;x<j;x++)
26                     dp[i][j]=max(dp[i][j],dp[i][x]+dp[x+1][j]);
27             }
28         }
29         printf("%d\n",dp[0][len-1]);
30     }
31     return 0;
32 }
				
时间: 2024-10-27 09:09:15

POJ 2955 Brackets (区间dp入门)的相关文章

POJ 2955 Brackets (区间DP)

题意:给定一个序列,问你最多有多少个合法的括号. 析:区间DP,dp[i][j] 表示在 第 i 到 第 j 区间内最多有多少个合法的括号. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <ios

POJ 2955 Brackets (区间dp 括号匹配)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3951   Accepted: 2078 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a reg

poj 2955 Brackets(区间DP求最长匹配子串)

思路:假设要求区间[i,j]的最长匹配字串,它必然可以从[i,j-1]转移而来,有可能是s[j]与s[i]发生"关系"(匹配或不匹配),一直到s[j-1],若不发生"关系",即s[j]跟自己发生"关系",用for循环枚举所有的可能,取最大值. 代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char

POJ 2955 Brackets 区间DP 最大括号匹配

http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08/09/2630497.html http://blog.csdn.net/chaiyuan414/article/details/5448699 #include <iostream> #include <string> #include <cstring> #inclu

Brackets POJ - 2955 (区间dp)

Brackets POJ - 2955 题意:给一个括号序列,问最多有多少个括号是可以配对的. 1 #include<cstdio> 2 #include<algorithm> 3 #include<string> 4 #include<iostream> 5 #include<cstring> 6 using namespace std; 7 const int maxn=110; 8 int dp[maxn][maxn]; 9 char s[

POJ 2955 Brackets 区间合并

输出一个串里面能匹配的括号数 状态转移方程: if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')             dp[i][j]=dp[i+1][j-1]+2; 然后再区间合并 1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交 2 #include<cstdio> 3 #include<cstring&

POJ 2955 Brackets (动规)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2999   Accepted: 1536 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a reg

POJ 2955 Brackets(计数问题吗呵呵)

我只能说这道题和上一道动态规划的问题真的是太像了,连方法也一模一样 确实,计数也需要存状态,计数也是需要动规的. 此时d[i][j]表示的状态是s[i~j]的序列中有多少 不规则 的括号. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n; char s[105]; int d[105][105]; bool match(char ch1,char ch2)

POJ 1179 Polygon 区间DP

链接:http://poj.org/problem?id=1179 题意:给出一个多边形,多边形的每个顶点是一个数字,每条边是一个运算符号"+"或者"x".要求的过程如下,手下移除一条边,即这条边不做运算.之后每次移除一条边,将其两边的数字进行对应边的运算,用得到的数字来替代原来的两个点.要求所有边都移除以后得到的最大的答案. 思路:典型的区间DP,在过程中每次操作的处理方式为dp_max[i][j]=dp[i][k]*dp[k+1][j],dp_max[i][j]

codeforces 149D - Coloring Brackets (区间dp)

题目大意: 给出一组合法的括号. 括号要么不涂颜色,要么就涂上红色或者绿色. 匹配的括号只能有一个有颜色. 两个相邻的括号不能有相同的颜色. 思路分析: 因为是一个合法的括号序列. 所以每个括号与之匹配的位置是一定的. 那么就可以将这个序列分成两个区间. (L - match[L] )  (match[L]+1, R) 用递归先处理小区间,再转移大区间. 因为条件的限制,所以记录区间的同时,还要记录区间端点的颜色. 然后就是一个递归的过程. #include <cstdio> #include