题目链接

问题描述 :

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\n",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;
}

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

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

    题目链接:http://poj.org/problem?id=1240 本文链接:http://www.cnblogs.com/Ash-ly/p/5482520.html 题意: 通过一棵二叉树的中序 ...

  2. MPI Maelstrom(East Central North America 1996)(poj1502)

    MPI Maelstrom 总时间限制:  1000ms 内存限制:  65536kB 描述 BIT has recently taken delivery of their new supercom ...

  3. poj 2732 Countdown(East Central North America 2005)

    题意:建一个家庭树,找出有第d代子孙的名字,按照要求的第d代子孙的数从大到小输出三个人名,如果有一样大小子孙数的,就按字母序从小到大将同等大小的都输出,如果小于三个人的就全输出. 题目链接:http: ...

  4. Gym-101673 :East Central North America Regional Contest (ECNA 2017)(寒假自训第8场)

    A .Abstract Art 题意:求多个多边形的面积并. 思路:模板题. #include<bits/stdc++.h> using namespace std; typedef lo ...

  5. 2017-2018 ACM-ICPC East Central North America Regional Contest (ECNA 2017) Solution

    A:Abstract Art 题意:给出n个多边形,求n个多边形分别的面积和,以及面积并 思路:模板 #include <bits/stdc++.h> using namespace st ...

  6. East Central North America Region 2015

    E 每过一秒,当前点会把它的值传递给所有相邻点,问t时刻该图的值 #include <iostream> #include <cstdio> #include <algo ...

  7. 2016-2017 ACM-ICPC East Central North America Regional Contest (ECNA 2016) F 区间dp

    Problem F Removal GameBobby Roberts is totally bored in his algorithms class, so he’s developed a li ...

  8. 2014-2015 ACM-ICPC East Central North America Regional Contest (ECNA 2014) A、Continued Fractions 【模拟连分数】

    任意门:http://codeforces.com/gym/100641/attachments Con + tin/(ued + Frac/tions) Time Limit: 3000/1000 ...

  9. East Central North America 2006 Hie with the Pie /// 状压dp oj22470

    题目大意: 输入n,有n个地方(1~n)需要送pizza pizza点为0点 接下来n+1行每行n+1个值 表示 i 到 j 的路径长度 输出从0点到各点送pizza最后回到0点的最短路(点可重复走) ...

随机推荐

  1. IE 低版本 透明度

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. BZOJ4553: [Tjoi2016&Heoi2016]序列 树套树优化DP

    把pos[i]上出现的平常值定义为nor[i]最大值定义为max[i]最小值定义为min[i],那么我们发现在两个值,i(前),j(后),当且仅当max[i]<=nor[j],nor[i]< ...

  3. 【刷题】BZOJ 4817 [Sdoi2017]树点涂色

    Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...

  4. 51nod 1421 最大MOD值 | 暴力

    题面 有一个a数组,里面有n个整数.现在要从中找到两个数字(可以是同一个) ai,aj ,使得 ai mod aj 最大并且 ai ≥ aj. Input 单组测试数据. 第一行包含一个整数n,表示数 ...

  5. Mac OS 装gdb

    1 要求按照mac ports 2 命令:sudo port install gdb 3 安装位置在: /opt/local/bin/ggdb , 注意,ggdb是执行命令

  6. Java之Junit和反射

    Junit,反射 Junit 1.测试的分类: 黑盒测试 : 不需要写代码,给输入值,看程序是否能够输出期望的值. 白盒测试 : 需要进行代码的编写,关注的是程序的具体流程. 2.使用步骤(方法类的命 ...

  7. Windows + Ubuntu下JDK与adb/android环境变量配置完整教程

    假设JDK和android sdk路径分别如下: D:\Program Files\Java\jdkD:\android-sdk 1.JDK环境变量配置JAVA_HOME=D:\Program Fil ...

  8. Android 资源目录 相关知识 raw、 drawable、 values..

    一定要看的 Android 资源目录的相关知识 raw drwable valueshttp://blog.csdn.net/shichexixi/article/details/5985677 ht ...

  9. centos详细安装redis步骤

    1. 从官网(http://redis.io)下载最新稳定版2. 使用命令解压下载的tar包:tar –zxvf redis-3.2.0.tar.gz3. 通过命令cd redis-3.2.0进入源码 ...

  10. 流媒体协议之RTSP服务端的实现20180629

    RtspServer是参考了live555和jrtplib实现的,但代码全部是重新书写的,所以不依赖于任何第三方库即可编译和运行, 目前仅支持h264和G711格式,这是rtp打包时决定的,后续将不断 ...