POJ 2955 Brackets (range dp matching), poj2955
Brackets
Time Limit:1000 MS |
|
Memory Limit:65536 K |
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,
- IfSIs a regular brackets sequence, then (S) And [S] Are regular brackets sequences, and
- IfAAndBAre regular brackets sequences, thenABIs 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 charactersA1A2...An, Your goal is to find the length of the longest regular brackets sequence that is a subsequenceS. That is, you wish to find the largestMSuch that for indicesI1,I2 ,...,ImWhere 1 ≤I1 <I2 <... <Im≤N,Ai1Ai2...AimIs 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, intrusive. The end-of-file is marked by a line containing the word "end" and shocould not be processed.
Output
For each input case, the program shocould print the length of the longest possible regular brackets subsequence on a single line.
Sample Input
((()))()()()([]]))[)(([][][)end
Sample Output
66406
Source
Stanford Local 2004
Question link: http://poj.org/problem? Id = 2955
Question: Give A sequence of parentheses and ask the maximum number of valid parentheses in the sequence. If A is legal, [A] and (A) are legal. If, if B is valid, AB is also valid.
Question Analysis: It is similar to the classical brackets matching in POJ 1141. This question is simpler and we want to solve the problem. Since the maximum number of brackets is required, we need to add the minimum number of brackets, so that the entire sequence is valid, this will be converted into the question 1141, and the maximum matching nature of the brain-dynamic analogy bipartite graph will be opened, the maximum matching + the Maximum Independent Set = points, obviously, if you want to add the least vertices to make the sequence legal, the least vertices to be added will be | the largest independent set |, which we need is the original sequence | the maximum match |, the above is purely yy, the following shows the transfer equation, which is exactly the same as 1141.
Dp [I] [I] = 1;
Then the enumerated range Length
1) peripheral matching: dp [I] [j] = dp [I + 1] [j-1];
2) The peripheral does not match. Enumeration Split points: 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 < len - l; i++) { int j = i + l; dp[i][j] = INF; if((s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']')) dp[i][j] = dp[i + 1][j - 1]; for(int k = i; k < j; k++) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]); } } printf("%d\n", len - dp[0][len - 1]); }}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.