博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
考研编程练习----m叉树先序和后序所包含的情况
阅读量:5290 次
发布时间:2019-06-14

本文共 2476 字,大约阅读时间需要 8 分钟。

题目描述:

        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 cba2 abc bca10 abc bca13 abejkcfghid jkebfghicda
样例输出:
4145207352860
经典代码:
 
#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

转载于:https://www.cnblogs.com/Alex0111/p/4622222.html

你可能感兴趣的文章
在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。...
查看>>
MVC,MVP 和 MVVM 的图示,区别
查看>>
IDEA快速实现接口快捷方式
查看>>
用默认的打开方式打开本地文件
查看>>
JavaScript-jQuery报TypeError $(...) is null错误(jQuery失效)解决办法
查看>>
C语言栈的实现
查看>>
代码为什么需要重构
查看>>
SAP销售模块塑工常见问题和解决方案(自己收藏)
查看>>
事后诸葛亮博客
查看>>
TC SRM 593 DIV1 250
查看>>
SRM 628 DIV2
查看>>
Round Numbers
查看>>
2018-2019-2 20165314『网络对抗技术』Exp5:MSF基础应用
查看>>
Varish 缓存
查看>>
Jbpm5.4实例在JBoss中运行、及H2数据库迁移oracle数据库
查看>>
各个平台的mysql重启命令
查看>>
统计单词,字符,和行
查看>>
蓝牙的几种应用层协议作用
查看>>
《Akka应用模式:分布式应用程序设计实践指南》读书笔记8
查看>>
jQuery垂直滑动切换焦点图
查看>>