poj 1240 DFS

10 篇文章 0 订阅

不知道为什么分到DP分类里面去了,(pos1,pos2)表示一颗子数的前序遍历  (pos3,pos4)表示该颗子数的后序遍历 

然后可以将该子树分成更小的子树,直到pos1==pos2

每次的种类数就是   子树的种类树相乘*C( M,子树个数)

AC代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

char s1[100], s2[100];
int M;

int Cn( int n, int k ){
    int ans = 1;
    for( int i = 1; i <= k; i++ ){
        ans *= n;
        n--;
    }
    for( int i = 1; i <= k; i++ ){
        ans /= i;
    }
    return ans;
}

int DFS( int pos1, int pos2, int pos3, int pos4 ){
    if( pos1 == pos2 ){
        return 1;
    }
    int a, b, c, d;
    a = b = pos1 + 1;
    c = d = pos3;
    int ans = 1, temp = 0;
    while( a <= pos2 ){
        while( s2[d] != s1[a] ) d++;
        b = a + d - c;
        ans *= DFS( a, b, c, d );
        temp++;
        a = b = b + 1;
        c = d = d + 1;
    }
    return ans * Cn( M, temp );
}

int main(){
    while( scanf( "%d", &M ) && M ){
        scanf( "%s%s", s1, s2 );
        cout << DFS( 0, strlen( s1 ) - 1, 0, strlen( s2 ) - 1 ) << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值