题目描述:

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 form
m s1 s2
        indicating 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 jkebfghicda
样例输出:
4
1
45
207352860
经典代码:
 
#include <stdio.h>
#include <string.h>
char pre[30], post[30];
int m;
int find(char* p, char x) {
    int i=0;
    while (p[i]!=x) i++;
    return i;
}
typedef long long ll;
ll C(int a, int b) {
    ll u = 1;
    ll d = 1;
    while (b) {
    u *= a--;
    d *= b--;
    }
    return u/d;
}
ll test(char* p, char* q, int n) {
    if (n==0) return 1;
    ll f = 1;
    int c = 0;
    int i;
    while (n) {
    c++;
    i = find(q,*p);
    f *= test(p+1,q,i);
    p += i+1;
    q += i+1;
    n -= i+1;
    }
    return f * C(m,c);
}
int main() {
    while(scanf("%d%s%s",&m,pre,post)==3) {
    printf("%lld\n",test(pre+1,post,strlen(pre)-1));
    }  
    return 0;
}
 
百度文库的解释:http://wenku.baidu.com/link?url=ZddYeW-pYEgst83coqElNsI-aHY_JwyuHwsKBHkrxPNWxYCMCn0ltDqq7K-IGdZkr48WdgG4chrIkS1h5cWUnhzPQbJBL3a4N_OLUffbe4i

考研编程练习----m叉树先序和后序所包含的情况的更多相关文章

  1. 【算法】二叉树、N叉树先序、中序、后序、BFS、DFS遍历的递归和迭代实现记录(Java版)

    本文总结了刷LeetCode过程中,有关树的遍历的相关代码实现,包括了二叉树.N叉树先序.中序.后序.BFS.DFS遍历的递归和迭代实现.这也是解决树的遍历问题的固定套路. 一.二叉树的先序.中序.后 ...

  2. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  3. [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  4. Java实现二叉树的前序、中序、后序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...

  5. Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的四种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序.层序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似, ...

  6. LeetCode(106):从中序与后序遍历序列构造二叉树

    Medium! 题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 posto ...

  7. 二叉排序树的构造 && 二叉树的先序、中序、后序遍历 && 树的括号表示规则

    二叉排序树的中序遍历就是按照关键字的从小到大顺序输出(先序和后序可没有这个顺序) 一.以序列 6 8 5 7 9 3构建二叉排序树: 二叉排序树就是中序遍历之后是有序的: 构造二叉排序树步骤如下: 插 ...

  8. JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)

    import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Queue; ...

  9. HDU 1710 二叉树遍历,输入前、中序求后序

    1.HDU  1710  Binary Tree Traversals 2.链接:http://acm.hust.edu.cn/vjudge/problem/33792 3.总结:记录下根结点,再拆分 ...

  10. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

随机推荐

  1. day13 多线程建立方法

    #01创建多线程    继承Thread类    覆盖run方法:run方法里面运行要执行的代码    创建对象    调用start方法,start方法会开启线程,然后调用run方法 获取线程名字: ...

  2. GTK+开发环境搭建

    GTK+开发环境搭建 "工欲善其事,必先利其器"首先介绍一下GTK+开发环境的搭建,网上很多所谓的GTK的开发环境的搭建基本都是抄来抄去,也不知道有没有人使用他们介绍的方法搭建并编 ...

  3. LANMP常用配置.md

    httpd 配置 MPM prefork StartServers # 服务器启动时建立的子进程数量. MinSpareServers # 空闲子进程的最小数量:如果当前空闲子进程数少于MinSpar ...

  4. linux 根据端口号查看占用进程的pid

    1.netstat -nap | grep 端口号 2.第一种的可以查看端口号对应的pid,但是会出现pid为空的情况,这种时候用:lsof -i:端口号 3.根据pid查看端口号 netstat - ...

  5. 【[SDOI2008]山贼集团】

    非常好的一道题 树上的状压\(dp\) 根据数据范围我们就能知道这是一道需要状压的题目 所以状态就是\(dp[i][S]\)表示在以\(i\)为根的子树里,选择的状态为\(S\)的最大收益 这个收益只 ...

  6. C/C++——老夫记不住

    常指针 Coordinate * const pCoor=&coor1; 指针所指向的地址不可改变. 常对象指针 const Coordinate *pCoor=&coor1; 所指对 ...

  7. Js 中的 this

    Js 中 this 的理解   this 是啥 ? this是 JavaScript 语言的一个关键字,它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用; 随着函数使用场合的不同,this ...

  8. 后台npm

    后台进程运行 nohupwhich nohup.bash_profile中并source加载 如果没有就安装吧yum provides */nohup nohup npm start &原程序 ...

  9. IIS中ASP.NET虚拟目录不继承主站点web.config设置的办法(转载)

    ASP.NET提供了强大的Web.config来配置网站,一般来说一个网站只有一个根目录下的Web.config文件,有时候我们希望子目录有着不同的权限或者参数设置,则可以在相应子目录增加一个Web. ...

  10. 闲话缓存:ZFS 读缓存深入研究-ARC(二)

    Solaris ZFS ARC的改动(相对于IBM ARC) 如我前面所说,ZFS实现的ARC和IBM提出的ARC淘汰算法并不是完全一致的.在某些方面,它做了一些扩展: ·         ZFS A ...