Title Link: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=15
The classic interval DP, which first enumerates the size of the interval and the left boundary of the interval, can be computed by the right boundary. First initialize a match, and that is to see if the two parentheses match, namely:
(S[i] = = ' (' && s[j] = = ') ') | | (S[i] = = ' [' && s[j] = = '] ')? DP (I,J) = DP (i+1,j-1) +2): DP (I,J) = 0
Next enumerate all points in the middle of I and J, update the DP (I,J) =max (DP (I,J), DP (I+M) +DP (m+1,j)) to find a possible better match.
1 /*2 ━━━━━┒ギリギリ♂eye! 3 ┓┏┓┏┓┃キリキリ♂mind! 4 ┛┗┛┗┛┃\0/5 ┓┏┓┏┓┃/6 ┛┗┛┗┛┃ノ)7 ┓┏┓┏┓┃8 ┛┗┛┗┛┃9 ┓┏┓┏┓┃Ten ┛┗┛┗┛┃ One ┓┏┓┏┓┃ A ┛┗┛┗┛┃ - ┓┏┓┏┓┃ - ┃┃┃┃┃┃ the ┻┻┻┻┻┻ - */ -#include <algorithm> -#include <iostream> +#include <iomanip> -#include <cstring> +#include <climits> A#include <complex> at#include <cassert> -#include <cstdio> -#include <bitset> -#include <vector> -#include <deque> -#include <queue> in#include <stack> -#include <ctime> to#include <Set> +#include <map> -#include <cmath> the using namespacestd; * #defineFr First $ #defineSC SecondPanax Notoginseng #defineCL Clear - #defineBUG puts ("Here!!!") the #defineW (a) while (a--) + #definePB (a) push_back (a) A #defineRint (a) scanf ("%d", &a) the #defineRll (a) scanf ("%i64d", &a) + #defineRs (a) scanf ("%s", a) - #defineCIN (a) CIN >> a $ #defineFRead () freopen ("in", "R", stdin) $ #defineFWrite () freopen ("Out", "w", stdout) - #defineRep (i, Len) for (int i = 0; i < (len); i++) - #defineFor (I, A, Len) for (int i = (a); I < (len); i++) the #defineCls (a) memset ((a), 0, sizeof (a)) - #defineCLR (A, X) memset ((a), (x), sizeof (a))Wuyi #defineFull (a) memset ((a), 0x7f7f7f, sizeof (a)) the #defineLRT RT << 1 - #defineRRT RT << 1 | 1 Wu #definePi 3.14159265359 - #defineRT return About #defineLowbit (x) x & (-X) $ #defineOnenum (x) __builtin_popcount (x) -typedefLong LongLL; -typedefLong DoubleLD; -typedef unsignedLong LongULL; Atypedef pair<int,int>PII; +typedef pair<string,int>psi; thetypedef PAIR<LL, Ll>PLL; -typedef map<string,int>MSI; $typedef vector<int>VI; thetypedef vector<ll>VL; thetypedef vector<vl>VVL; thetypedef vector<BOOL>vb; the - Const intMAXN =1100; in intDP[MAXN][MAXN]; the CharS[MAXN]; the intN; About the intMain () { the //FRead (); the intT; + Rint (T); - W (T) { theRs (s); n =strlen (s);Bayi Cls (DP); theFor (K,2, n+1) { theRep (i, n-k+1) { -dp[i][i+k-1] =0; - intj = i + K-1; the if((s[i] = ='('&& S[j] = =')') || (S[i] = ='['&& S[j] = =']')) { theDP[I][J] = dp[i+1][j-1] +2; the } the for (M, I, j) { -DP[I][J] = max (Dp[i][j], dp[i][m] + dp[m+1][j]); the } the } the }94printf"%d\n", n-dp[0][n-1]); the } theRt0; the}
[NYIST15] Bracket matching (b) (interval DP)