Code vs 1029 Calendar Problem interval DP

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.