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 regular brackets sequence, then (s) and [s] is regular brackets sequences, and
 If a and b are regular brackets sequences, thenAB is a regular brackets sequence.
 No other sequence is a regular brackets sequence
For instance, all of the following character sequences is regular brackets sequences:
(), [], (()), ()[], ()[()]
While the following character sequences is not:
(, ], )(, ([)], ([(]
Given a brackets sequence of characters a1a2 ... An, your goal are to find the length of the longest regular brackets sequence, which is a subsequence ofs. That's, you wish to find the largest m such this for indicesi1, i2, ..., im where 1 ≤i1 < i2 < ... < im ≤ n, ai1ai 2 ... aim is a regular brackets sequence.
Given the initial sequence ([([]])]
, the longest regular brackets subsequence is [([])]
.
Input
The input test file would contain multiple test cases. Each input test case consists of a single line containing only the characters (
, )
, [
, and ]
; St'll has length between 1 and inclusive. The Endoffile is marked by a line containing the word "end" and should not being 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
((())) () () () () ([][][)
Sample Output
66406
Source
Stanford Local 2004
Title Link: http://poj.org/problem?id=2955
Title: Give a sequence of parentheses, ask the number of valid parentheses in the sequence, if a is legal, then [a], (a) are legal, if A B is legal, AB is also legal
Title Analysis: and POJ 1141 that classic bracket matching similar, the problem is more simple, to find a way to transform the problem, since the maximum number of brackets required to match, we consider adding a minimum of parentheses, so that the entire sequence is legal, so it turned into 1141 that problem, The nature of the maximal matching of the brainmotion analogy binary graph, the maximum match + the maximum independent set = the number of points, obviously to add the least points to make the sequence legal, the least number of points is the  maximum independent set , we require the original sequence of  maximum Match , the above is purely yy, the following gives the transfer equation, and 11,411
Dp[i][i] = 1;
Then enumerate the interval lengths
1) Peripheral matching: dp[i][j] = dp[i + 1][j1];
2) perimeter mismatch, enumeration split point: Dp[i][j] = min (Dp[i][j], dp[i][k] + dp[k + 1][j]); (I <= K < j)
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const INF = 0x3fffffff ; Char s[205];int dp[205][205];int main () {while (scanf ("%s", s)! = EOF && strcmp (S, "end")! = 0) { int Len = strlen (s); memset (DP, 0, sizeof (DP)); for (int i = 0; i < len; i++) dp[i][i] = 1; for (int l = 1, l < len; l++) {for (int i = 0; i < lenl; i++) { Int j = i + L; DP[I][J] = INF; if ((s[i] = = ' (' && s[j] = = ') ')   (S[i] = = ' [' && s[j] = = '] ')) DP[I][J] = dp[i + 1][j1]; for (int k = i; k < J; k++) dp[i][j] = min (Dp[i][j], dp[i][k] + dp[k + 1][j]); } } printf ("%d\n", Lendp[0][len1]);} }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2955 Brackets (interval dp bracket matching)