POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)

题目链接

问题描述 :

We are all familiar with pre-order, in-order and post-order traversals of binary trees. A common problem in data structure classes is to find the pre-order traversal of a binary tree when given the in-order and post-order traversals. Alternatively, you can find the post-order traversal when given the in-order and pre-order. However, in general you cannot determine the in-order traversal of a tree when given its pre-order and post-order traversals. Consider the four binary trees below:

All of these trees have the same pre-order and post-order traversals. This phenomenon is not restricted to binary trees, but holds for general m-ary trees as well.

输入:

Input will consist of multiple problem instances. Each instance will consist of a line of the formm s1 s2indicating that the trees are m-ary trees, s1 is the pre-order traversal and s2 is the post-order traversal.All traversal strings will consist of lowercase alphabetic characters. For all input instances, 1 <= m <= 20 and the length of s1 and s2 will be between 1 and 26 inclusive. If the length of s1 is k (which is the same as the length of s2, of course), the first k letters of the alphabet will be used in the strings. An input line of 0 will terminate the input.

输出:

For each problem instance, you should output one line containing the number of possible trees which would result in the pre-order and post-order traversals for the instance. All output values will be within the range of a 32-bit signed integer. For each problem instance, you are guaranteed that there is at least one tree with the given pre-order and post-order traversals.

样例输入:

2 abc cba
2 abc bca
10 abc bca
13 abejkcfghid jkebfghicda0

样例输出:

4
1
45
207352860

题意:
一般来说我们可以根据一棵树的中序遍历序列和后序序列(前序序列)来唯一的确定一个树的结构,但是没有办法通过一个树的前序序列和后序序列来确定这棵树。因为这样的树可能有很多种不同的形态,现在给定一棵树是n次树,以及这棵树的前序和后序序列,要求计算出这样的树有多少中可能。

分析:
首先对于m叉树的前序遍历序列,第一个字符一定表示这颗m叉树的根,在后序遍历序列中最后一个字符表示m叉树的根.前序遍历序列中的第二个字符x1,一定是m叉树根节点的第一棵子树的根节点,那么在后序遍历序列中,从开始部分到x1的部分一定是m叉树的第一颗子树的后序遍历序列,假设,这部分的个数为n1,那么在前序遍历序列中,从x1开始后的n1个字符一定是m叉树的第一颗子树的前序遍历序列.则在前序遍历序列中截掉这n1个字符以及代表根的字符后,在剩下的序列中,第一个字符x2一定是m叉树的第二棵子树的根节点.在后序遍历序列剩余的部分中,从头到x2的部分即是这第二颗子树的后序遍历序列,设节点个数为n2.那么在前序遍历序列中,从x2开始的n2个字符一定是这m叉树第二颗子树的前序遍历序列.以此类推,对于每棵树的前序和后序遍历序列,可以确定根节点的子节点是哪些,并且能够得到分别以这些子节点为根节点的子树的前序和后序遍历序列,如此的递归下去,可以知道这m叉树的层次结构和节点之间的父子关系以及兄弟节点之间的顺序关系.

  节点之间的关系确定之后,需要确定一共有多少这样的m叉树.首先对于二叉树的情况,当一个根节点只有一个子节点时,这个儿子节点位于左二子或者右儿子的位置都会使得整个二叉树不同.那么类比到m叉树,如果m叉树某一个根节点只有n个子节点,那么这n个节点分别属于哪个树杈都会使得整个树的形状不一样.由于这n个节点的顺序是确定的,相当于把n个点顺序的放到m个位置,则有C(m , n)中放法.对于整棵树来说,树的种数等于每个节点的子节点位置的种数的乘积.

例如:

13 abejkcfghid jkebfghicda

'a'是这13叉树的根节点,在前序遍历序列中'b'为这13叉树根节点'a'的第一颗子树的根,则在后序遍历序列中从'j'到'b'则为这第一棵子树的后序遍历序列,经计算共有四个节点,那么在前序遍历序列中,从'b'到'k'的这四个字符一定是第一颗子树的前序遍历序列.前序遍历序列中'k'后面的第一个字符'c',一定是这颗13叉树根节点'a'的第二颗子树的根,则在后序遍历序列中从'f'到'c'的五个字符一定是第二棵子树的后序遍历序列,那么在前序遍历序列中,从'c'往后再截取五个字符到'i',则说明这部分为第二棵子树的前序遍历序列.'i'之后的第一个字符'd'则为根节点'a'的第三棵子树的根节点,同样在后序遍历序列中'd'则为第三棵子树的后序遍历序列,在这里则说明第三棵子树只有一个根节点'd'.然后分别对第一颗子树和第二课子树前序以及后序遍历序列进行递归,从而得到子树根节点的子节点的个数以及顺序.由于根节点'a'有'b','c','d'这三个子节点,那么对于13叉树来说要把这三个节点顺序的放到13个位置则为C(13, 3).对于'a'的子节点'b'来说很明显仅有一个子节点'e',所以同样有C(13, 1)种放法,对于'e'节点则有两个子节点'j'和'k',那么种类为C(13, 2).对于'a'的第二个子节点'c'来说有4个子节点'f','g','h'及'i',那么总放法为C(13, 4),所以总的种数位C(13, 3) * C(13, 1) *C(13, 2) * C(13, 4) = 207352860.

代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
typedef long long LL;
using namespace std;
int n;
LL ans;
char pre[30],epi[30];

LL calculate(LL n, LL m) //计算组合数,一共有n个位置可以放,但是只放了m个位置
{
    if(m < n - m) m = n - m;
    LL ans = 1;
    for(LL i = m + 1; i <= n; i++) ans *= i;
    for(LL i = 1; i <= n - m; i++) ans /= i;
    return ans;
}
void research(int preLeft,int preRight,int epiLeft,int epiRight)
{
    int i,cnt=0,root=preLeft+1;//root表示的是以pre[preLeft]为根节点的直接孩子节点的下标
    while(root<=preRight)
    {
        for(i=epiLeft;i<=epiRight;i++)//在后序序列中找到当前的根节点的位置,
        {
            if(epi[i]==pre[root])
                break;
        }
        int Size=i-epiLeft+1;//Size为以root为根节点的整个子树的大小
        research(root,root+Size-1,epiLeft,i);//递归这往下寻找这棵子树
        //pre[root~ (root + Size - 1)] 为以pre[root]为根节点的子树的前序遍历序列, epi[epiLeft~i]则为其的后序遍历序列

        cnt++;//最起始的根节点的子树个数加
        root+=Size;//再次递归最起始节点的下一个子树
        epiLeft=i+1;////截掉后序遍历序列中epiLeft ~ i 的部分
    }
    ans*=calculate((LL)n,LL(cnt));//累乘计算答案数
}

void solve()
{
    int len=strlen(pre);
    research(0,len-1,0,len-1);//当前的线序和后续的区间
    printf("%lld
",ans);
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        memset(pre,0,sizeof(pre));
        memset(epi,0,sizeof(epi));
        scanf("%s %s",pre,epi);
        ans=1;
        solve();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/cmmdc/p/7707931.html