Poj 2955 Brackets interval dp
The question is to give you a string consisting of braces and parentheses. Ask how long the longest subsequence in the string conforms to the matching rules of mathematical parentheses.
At the beginning, I plan to use the legendary left-closed and right-open interval to write data. Later I found it was not suitable for me, but I still wrote back the left-closed and right-closed interval.
The idea of dp is relatively simple. dp [I] [j] indicates that the string from I to j is the oldest sequence matching brackets. For any interval, a vertex k (I <= k <j) can divide the interval into two parts: [I, k] and [k + 1, j.
So we get the transfer equation: dp [I] [j] = max (dp [I] [k] + dp [k + 1] [j])
In addition, if the parentheses of I and j match, there is another situation here, that is, dp [I] [j] = max (dp [I-1] [j + 1] + 2)
Then the memory will work. (I feel like this question is more memorable)
#include
#include
#include using namespace std;string a;int dp[101][101];bool judge(char a, char b) { if (a == '(' && b == ')') return true; if (a == '[' && b == ']') return true; return false;}int DP(int l, int r) { if (dp[l][r] != -1) { return dp[l][r]; }www.bkjia.com if (l == r) { return dp[l][r] = 0; } int tmp = 0; for (int k=l; k
> a) { if (a == end) break; memset(dp, -1, sizeof(dp)); int ans = DP(0, a.size()-1); cout << ans << endl; } return 0;}