码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2955-Brackets(区间DP)

时间:2014-11-08 23:41:30      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   os   sp   for   

Brackets
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3340   Accepted: 1716

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, …, imwhere 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[l][r]为区间[l,r]内括号匹配数,dp[l][r]=max(dp[l][r],dp[l+1][k-1]+dp[k+1][r]+2)(s[l]和s[k]匹配)
枚举区间,形式大都一样,3层for循环,看到很多人说区间DP写成记忆化搜索比较容易懂。。orz
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define ll __int64
#define pp pair<int,int>
using namespace std;
const int INF = 0x3f3f3f3f;
char s[110];
int dp[110][110];
void solve()
{
	memset(dp, 0, sizeof(dp));
	int len = strlen(s) - 1;

	for (int p = 1; p <= len; p++) {
		for (int l = 1; l <= len; l++) {
			int r = l + p - 1;

			if (r > len) {
				break;
			}

			dp[l][r] = dp[l + 1][r];

			for (int k = l + 1; k <= r; k++)
				if ((s[l] == '(' && s[k] == ')') || (s[l] == '[' && s[k] == ']')) {
					dp[l][r] = max(dp[l][r], dp[l + 1][k - 1] + dp[k + 1][r] + 2);
				}
		}
	}

	printf("%d\n", dp[1][len]);
}
int main()
{

	while (~scanf("%s", s + 1) && s[1] != 'e') {
		s[0] = 2;
		solve();
	}

	return 0;
}

POJ 2955-Brackets(区间DP)

标签:des   style   blog   io   color   ar   os   sp   for   

原文地址:http://blog.csdn.net/qq_16255321/article/details/40930175

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!