时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:701

解决:398

题目描述:

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
来源:
2008年上海交通大学计算机研究生机试真题

思路:

求对于m叉树而言其每层的叶子节点的组合方式有多少种。

由先序和后序序列其实可以却确定每一层的叶子节点的个数,以及哪些是这一层的叶子节点,唯一不确定的就是这些节点的位置(但是由先序可以确定这些叶子节点相对位置是确定的),比如第i层有n个叶子节点(由先序和后序结合判定出),那么这层就有c(n,m)种组合方式,然后确定某个叶子节点的子树,对其进行递归求解。

代码:

#include <stdio.h>
#include <string.h> #define M 20
#define N 26 int m; long long C(int m, int k)
{
int i;
long long c = 1;
for (i=m; i>k; i--)
c *= i;
for (i=m-k; i>0; i--)
c /= i;
return c;
} long long prepost(char s1[], char s2[])
{
int len = strlen(s1);
if (len == 0 || len == 1)
return 1; char root;
int c;
char sch1[M][N+1], sch2[M][N+1];
int i, j, k;
c = 0;
i = 1;
while (i < len)
{
root = s1[i];
j = i-1;
k = 0;
do
{
sch1[c][k] = s1[i];
sch2[c][k] = s2[j];
k++;
i++;
} while (s2[j++] != root);
sch1[c][k] = '\0';
sch2[c][k] = '\0';
c++;
} long long count = C(m, c);
for (i=0; i<c; i++)
count *= prepost(sch1[i], sch2[i]);
return count;
} int main(void)
{
char s1[N+1], s2[N+1]; while (scanf("%d", &m) != EOF)
{
scanf("%s%s", s1, s2);
//printf("%lld\n", C(6, 2));
printf("%lld\n", prepost(s1, s2));
} return 0;
}
/**************************************************************
Problem: 1044
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:912 kb
****************************************************************/

九度OJ 1044:Pre-Post(先序后序) (n叉树、递归)的更多相关文章

  1. 九度OJ 1360:乐透之猜数游戏 (递归)

    时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:955 解决:261 题目描述: 六一儿童节到了,YZ买了很多丰厚的礼品,准备奖励给JOBDU里辛劳的员工.为了增添一点趣味性,他还准备了一些 ...

  2. 九度OJ 1358:陈博的平均主义 (遍历、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:354 解决:191 题目描述: 在JOBDU团队里,陈博是最讲平均主义的人了,但并不是像梁山好汉那样能够做到有钱同花,有肉同吃,毕竟,他还是 ...

  3. 九度OJ 1146:Flipping Pancake(翻饼子) (递归、游戏)

    时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:265 解决:116 题目描述: We start with a stack n of pancakes of distinct sizes. ...

  4. 九度OJ 1124:Digital Roots(数根) (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2963 解决:1066 题目描述: The digital root of a positive integer is found by s ...

  5. 【九度OJ】题目1201:二叉排序树 解题报告

    [九度OJ]题目1201:二叉排序树 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1201 题目描述: 输入一系列整数,建立二叉排序 ...

  6. 【九度OJ】题目1078:二叉树遍历 解题报告

    [九度OJ]题目1078:二叉树遍历 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历 ...

  7. 【九度OJ】题目1065:输出梯形 解题报告

    [九度OJ]题目1065:输出梯形 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1065 题目描述: 每组测试 ...

  8. 【九度OJ】题目1061:成绩排序 解题报告

    [九度OJ]题目1061:成绩排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1061 题目描述: 有N个学 ...

  9. 【九度OJ】题目1118:数制转换 解题报告

    [九度OJ]题目1118:数制转换 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1118 题目描述: 求任意两个不同进制非 ...

随机推荐

  1. Codeforces 703D Mishka and Interesting sum(离线 + 树状数组)

    题目链接  Mishka and Interesting sum 题意  给定一个数列和$q$个询问,每次询问区间$[l, r]$中出现次数为偶数的所有数的异或和. 设区间$[l, r]$的异或和为$ ...

  2. SDOI 2015 约束个数和

    Description: 共\(T \le 5 \times 10^4\)组询问, 每组询问给定\(n\)和\(m\), 请你求出 \[ \sum_{i = 1}^n \sum_{j = 1}^m \ ...

  3. OBS插件开发以及OBS插件的选择(obs直播插件)研究思路

    obs版本的选择: 工作室版,优化了很多东西,缺点是不能用插件,在部分机型不稳定,因为更新的很频繁.不过这个插件不能用的说法还是停留在早起,截至到今天已经完美支持,所以在不久的将来会越来越好,如果是开 ...

  4. mysql之日期函数

    写在前面 mysql的学习,断断续续,今天就接着学习mysql的日期操作吧. 系列文章 mysql之创建数据库,创建数据表 mysql之select,insert,delete,update mysq ...

  5. 慕课网python进阶函数式编程学习记录

    函数 不等于 函数式 函数: function 函数式: functional,一种编程范式 就好比计算机 不等于 计算 c语言: 函数 python :函数式(计算) 函数式编程特点: 把计算视为函 ...

  6. oracle手动修改listener.ora和tnsnames.ora之后竟然无效

    oracle手动修改listener.ora和tnsnames.ora之后竟然无效 花式重启都没有生效,需要使用Net Configuration Assistant来进行刷一下,重新生成的监听还是一 ...

  7. DevExpress控件之TreeList

    基于v18.1 使用AppendNode方法手动赋值时,首先要添加treeListColumn 默认样式                                     修改后的样式   1 ...

  8. Android Touch事件传递机制具体解释 下

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38025165 资源下载:http://download.csdn.net/detail/yu ...

  9. 为什么阿里巴巴不建议在for循环中使用"+"进行字符串拼接

    字符串,是Java中最常用的一个数据类型了.关于字符串的知识,作者已经发表过几篇文章介绍过很多,如: Java 7 源码学习系列(一)--String 该如何创建字符串,使用" " ...

  10. win10 1709正式版iso镜像下载|windows10 1709秋季创意者更新官方下载地址

    win10 1709正式版iso镜像下载|windows10 1709秋季创意者更新官方下载地址 发布时间:2017-10-18 14:27发布者:系统城-xtcjh浏览数:74458 win10 1 ...