http://codevs.cn/problem/1029/
Given a binary tree (the node is lowercase characters) in accordance with the first order traversal and subsequent traversal of the resulting string, in fact, how many and two fork tree of the first order traversal and post-sequence traversal to satisfy the two strings.
The interval DP:DP (L, R, a, b) represents the number of scenarios in which the (L, r) segment of the S string matches the (A, b) segment of the T string. Then S[l] and t[b] must be the same, because these two are the root nodes of this section. Then we enumerate (l,r) the Zuozi (l+1,k) and (b) Zuozi (A,A+K-L-1);
DP (L,R,A,B) = SUM (DP (L+1,K,A,A+K-L-1) *DP (k+1,r,a+k-l,b-1));
Note that there is only one subtrees tree in the case of DP (L+1,R,A,B-1), which is the same in the Saozi right subtree is the same so DP (L,R,A,B) +=DP (l+1,r,a,b-1) * *, then happy memories of it.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;string _a, _b;long long Dp[30][30][30][30];long long dp (int l, int r, int a, int b) {//cout << L << "" << r << "" << a << "<< b << Endl; if (L > R) return 1; if (dp[l][r][a][b]! =-1) return dp[l][r][a][b]; if (_a[l]! = _b[b]) return dp[l][r][a][b] = 0; if (L = = r) Return dp[l][r][a][b] = 1; Long Long res = DP (l+1, R, A, b-1) * 2; for (int k=l+1; k<r; k++) { res + = DP (l+1, K, A, A+K-L-1) * DP (k+1, R, A+k-l, b-1); } return dp[l][r][a][b] = res;} int main () { cin >> _a >> _b; Memset (DP,-1, sizeof (DP)); cout << DP (0, _a.size ()-1, 0, _b.size ()-1) << Endl; return 0;}
Code vs 1029 Calendar Problem interval DP