链接:http://poj.org/problem?id=2955
Brackets
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4378 | Accepted: 2331 |
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 i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, 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
大意——给你一个长度不超过100的括号序列,求最长合法括号子序列的长度。合法的序列判定为:1.空的序列是合法的;2.如果一个序列s是合法的,那么(s)和[s]都是合法的;3.如果两个序列a和b分别都是合法的,那么ab也是合法的。例如:(), [], (()), ()[], ()[()]都是合法的,而(, ], )(, ([)], ([(]都不是合法的。
思路——典型区间DP模型。分析问题可以发现:如果找到一对匹配的括号[xxx]oooo,就把区间分成两部分,一部分是xxx,另一部分是oooo。设dp[i][j]表示区间[i,j]之间的最长合法括号子序列长度,那么当i<j时,如果区间[i+1,j]内没有与i匹配的括号,则dp[i][j]=dp[i+1][j];如果存在一个匹配的k,那么dp[i][j] = max{dp[i+1][j],dp[i+1][k-1]+dp[k+1][j]+
2(i+1<k<j&&i和k是一对匹配的括号)}。因此,我们把i从串末枚举到串首即可,最后dp[0][len-1]即为答案,len表示串长度。
复杂度分析——时间复杂度:O(len^3),空间复杂度:O(len^2)
附上AC代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
typedef unsigned int li;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double pi = acos(-1.0);
const double e = exp(1.0);
const double eps = 1e-8;
const int maxlen = 105;
char str[maxlen];
int dp[maxlen][maxlen];
bool match(char a, char b); // 是否满足括号匹配
int main()
{
ios::sync_with_stdio(false);
while (scanf("%s", str) && strcmp("end", str))
{
memset(dp, 0, sizeof(dp)); // 初始化
int len = strlen(str);
for (int i=len-1; i>=0; i--)
{ // 从后面不断扩大区间
for (int j=i+1; j<len; j++)
{ // dp[i][j]表示区间[i,j]之间的最长合法括号子序列长度
dp[i][j] = dp[i+1][j]; // 区间[i+1,j]没有与i匹配的括号
for (int k=i+1; k<=j; k++) // 寻找到匹配,一分为二(匹配内外)
if (match(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;
}
bool match(char a, char b)
{
if ((a=='('&&b==')') || (a=='['&&b==']'))
return 1;
return 0;
}