题目链接

问题描述 :

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.

代码:

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string.h>
  4. typedef long long LL;
  5. using namespace std;
  6. int n;
  7. LL ans;
  8. char pre[30],epi[30];
  9. LL calculate(LL n, LL m) //计算组合数,一共有n个位置可以放,但是只放了m个位置
  10. {
  11. if(m < n - m) m = n - m;
  12. LL ans = 1;
  13. for(LL i = m + 1; i <= n; i++) ans *= i;
  14. for(LL i = 1; i <= n - m; i++) ans /= i;
  15. return ans;
  16. }
  17. void research(int preLeft,int preRight,int epiLeft,int epiRight)
  18. {
  19. int i,cnt=0,root=preLeft+1;//root表示的是以pre[preLeft]为根节点的直接孩子节点的下标
  20. while(root<=preRight)
  21. {
  22. for(i=epiLeft;i<=epiRight;i++)//在后序序列中找到当前的根节点的位置,
  23. {
  24. if(epi[i]==pre[root])
  25. break;
  26. }
  27. int Size=i-epiLeft+1;//Size为以root为根节点的整个子树的大小
  28. research(root,root+Size-1,epiLeft,i);//递归这往下寻找这棵子树
  29. //pre[root~ (root + Size - 1)] 为以pre[root]为根节点的子树的前序遍历序列, epi[epiLeft~i]则为其的后序遍历序列
  30. cnt++;//最起始的根节点的子树个数加
  31. root+=Size;//再次递归最起始节点的下一个子树
  32. epiLeft=i+1;////截掉后序遍历序列中epiLeft ~ i 的部分
  33. }
  34. ans*=calculate((LL)n,LL(cnt));//累乘计算答案数
  35. }
  36. void solve()
  37. {
  38. int len=strlen(pre);
  39. research(0,len-1,0,len-1);//当前的线序和后续的区间
  40. printf("%lld\n",ans);
  41. }
  42. int main()
  43. {
  44. while(~scanf("%d",&n)&&n)
  45. {
  46. memset(pre,0,sizeof(pre));
  47. memset(epi,0,sizeof(epi));
  48. scanf("%s %s",pre,epi);
  49. ans=1;
  50. solve();
  51. }
  52. return 0;
  53. }

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. ubuntu下安装vsftpd及vsftpd配置文件不见的解决办法

    利用命令 sudo apt-get install vsftpd   //安装 进入etc文件可以找到 vsftpd.conf的配置文件 作为新手难免会弄错配置又不知道怎么办,那么可能会利用 sudo ...

  2. js & 快捷键 & vue bind bug

    js & 快捷键 & vue bind bug how to prevent addEventListener bind many times solution dataset &am ...

  3. java 创建过程

  4. Machine Learning CodeForces - 940F(带修改的莫队)

    题解原文地址:https://www.cnblogs.com/lujiaju6555/p/8468709.html 给数组a,有两种操作,1 l r查询[l,r]中每个数出现次数的mex,注意是出现次 ...

  5. Oracle 事务实例(非理论)

     begin    begin    savepoint  p1;    ---------============   在这里写删改差语句(SELECT 不行)每句以分号结尾:如 delete ta ...

  6. (转)enable_from_this方法的使用与陷阱

    转自http://blog.chinaunix.net/uid-442138-id-2122464.html   enable_from_this 的使用与实现原理说明:   shared_from_ ...

  7. 【BZOJ2024】舞会(动态规划,容斥,高精度)

    [BZOJ2024]舞会(动态规划,容斥,高精度) 题面 BZOJ 洛谷 题解 这种关系显然要先排序才不会不想影响. 设\(f[i][j]\)表示前\(i\)个女生中,选了\(j\)个女生配对,并且女 ...

  8. Luogu 1429 平面最近点对 | 平面分治

    Luogu 1429 平面最近点对 题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的 输入输出格式 输入格式: 第一行:n:2≤n≤200000 ...

  9. 洛谷 P1446 [HNOI2008]Cards 解题报告

    P1446 [HNOI2008]Cards 题目描述 小春现在很清闲,面对书桌上的\(N\)张牌,他决定给每张染色,目前小春只有\(3\)种颜色:红色,蓝色,绿色.他询问Sun有多少种染色方案,Sun ...

  10. 设置debian6源

    debian6真的太老了,个人喜欢追新,因为新版本用起来少很多乱七八糟的问题,不过线上有机器跑的这个版本,SO... 1.修改源文件,注释掉不可用的源,添加可用的源 # vim /etc/apt/so ...