POJ2955——Brackets

Brackets

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3341   Accepted: 1717

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 ofs. That is, you wish to find the largest
m such that for indicesi1, 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

Source

Stanford Local 2004

也是一道十分经典的区间dp题,我们用dp[i][j]表示从i到j,最大括号匹配数

如果第i个括号无法在[i+1, j]中匹配,那么dp[i][j] = dp[i+1][j];

否则如果在区间[i,j]中找到一个k,使得i和k配对,那么区间就被划分为2段,[i+1, k - 1]和[k+1,j]

所以dp[i][j] = max(dp[i +1][j], dp[i + 1][k - 1] + dp[k +1][j] + 2)

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>  

using namespace std;

char str[110];
int dp[110][110];

int main()
{
	while (~scanf("%s", str), str[0] != 'e')
	{
		int len = strlen(str);
		memset (dp, 0, sizeof(dp));
		for (int i = len - 1; i >= 0; --i)
		{
			for (int j = i + 1; j < len; ++j)
			{
				dp[i][j] = dp[i + 1][j];
				for (int k = i + 1; k <= j; ++k)
				{
					if ((str[i] == '(' && str[k] == ')') || (str[i] == '[' && str[k] == ']'))
					{
						dp[i][j] = max(dp[i][j], dp[i + 1][k - 1] + dp[k + 1][j] + 2);
					}
				}
			}
		}
		printf("%d\n", dp[0][len - 1]);
	}
	return 0;
}
时间: 2024-10-13 16:30:48

POJ2955——Brackets的相关文章

poj2955 Brackets

题意:就是数有一个字符串中有多少括号匹配:① ()算两种,② [ ] 算两种 题解: 和 LightOj 那道题一样   F[ i ][ j ] = max(F[ i + 1][ j ], F[ i + 1][ k - 1] + F[ k ][ j ] + 2) {i + 1 <= k <= j}; CODE: /* Author: JDD PROG: poj2955 Brackets DATE: 2015.10.8 */ #include <cstdio> #include &l

区间DP基础篇之 POJ2955——Brackets

怒拿一血,first blood,第一个区间DP,第一次就这样子莫名其妙不知不觉滴没了~~~ 题目虽然是鸟语,但还是很赤裸裸的告诉我们要求最大的括号匹配数,DP走起~ dp[i][j]表示区间[i,j]的最大匹配数,那么最重要的状态转移方程就是: dp[i][j]=max(dp[i][k]+dp[k+1][j]) 对啦,要先初始化边界啊,两步走~: memset(dp,0,sizeof dp); if str[i]==str[i+1]   则:dp[i][i+1]=2       请看---->

poj2955 Brackets (区间dp)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3571   Accepted: 1847 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(括号匹配问题)

题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: 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, th

poj2955 Brackets 简单区间dp

// poj2955 简单区间dp // d[i][j]表示i到j区间所能形成的最大匹配序列 // dp[i][j] = max(dp[i][k]+dp[k+1][j]){i<k<j} // dp[i][j] = max(dp[i+1][j-1]+2) if (s[i] match s[j]) // // 记忆化搜索的时候,将dp[i][i] = 0 ,其他赋值成-1; // // 做题的时候刚开始将dp[i][j]弄成0了,结果一直tle // 最后发现有好多的状态重复计算了,所以会tle

POJ2955 Brackets 题解 区间DP

题目链接:http://poj.org/problem?id=2955[题目描述]<规则的括号序列>我们定义一个字符串序列为“规则的括号序列”当且仅当它满足如下条件:1.空字符串是规则的括号序列:2.如果字符串 s 是一个规则的括号序列,那么 (s) 和 [s] 也是规则的括号序列:3.如果字符串 a 和 b 都是规则的括号序列,那么 ab 也是规则的括号序列:4.除此之外的字符串都不能称为规则的括号序列.举个例子,下面的这些字符串都是规则的括号序列: (), [], (()), ()[],

POJ2955 Brackets(区间DP)

给一个括号序列,求有几个括号是匹配的. dp[i][j]表示序列[i,j]的匹配数 dp[i][j]=dp[i+1][j-1]+2(括号i和括号j匹配) dp[i][j]=max(dp[i][k]+dp[k+1][j])(i<=k<j) 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 char str[111]; 6 int d[111

[poj2955]Brackets 括号最大匹配

括号最大匹配 区间dp 原题 dp[i][j] 表示区间 i~j 的区间最大括号匹配数 考虑dp[i][j]如何转移 对于 dp[i][j] , 可以从 dp[i + 1][j] 转移过来 首先是不匹配的情况 显然 dp[i][j] = dp[i + 1][j] 然后开始在 (i + 1)~j 枚举每一位字符 如果第 k 位的 str[k] 与 str[i] 相匹配 , 那么有 dp[i][j] = max (dp[i][j] , dp[i + 1][k - 1] + dp[k + 1][j]

poj2955: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