POJ 1240 Pre-Post-erous!
k叉树的前序和后续遍历,问一共有多少种这样的k叉树
这个就是树的同构,组合数就能解决
同样的题目在51nod也有的,我的另一篇博客
POJ 1240 Pre-Post-erous!
a a a a
/ / \ \
b b b b
/ \ / \
c c c c
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
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.
Output
Sample Input
2 abc cba
2 abc bca
10 abc bca
13 abejkcfghid jkebfghicda
0
Sample Output
4
1
45
207352860
看看大佬写的代码终于懂了
#include<stdio.h>
#include<string.h>
char s[],c[];
int m,l;
int C(int a,int b)
{
long long ans=;
for(int i=a-b+; i<=a; i++)ans*=i;
for(int i=; i<=b; i++)ans/=i;
return (int)ans;
}
int f(int l,char* s,char* c)
{
if(!l)return ;
int cur=,num=,ans=;
while(cur<l)
{
for(int i=cur; i<l; i++)
if(c[i]==s[cur])
{
num++;
ans*=f(i-cur,s+cur+,c+cur);
cur=i+;
break;
}
}
return ans*=C(m,num);
}
int main()
{
while(scanf("%d",&m),m)
{
scanf("%s%s",s,c);
int l=strlen(s);
printf("%d\n",f(l-,s+,c));
}
return ;
}
POJ 1240 Pre-Post-erous!的更多相关文章
- 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 题意: 通过一棵二叉树的中序 ...
- POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)
题目链接 问题描述 : We are all familiar with pre-order, in-order and post-order traversals of binary trees. ...
- POJ 1240 Pre-Post-erous! 解题报告
题意: 给出一个m叉树的前,后序遍历求这样的树有多少种. Solution: 我们知道前序遍历的第一个点一定是根节点,后序遍历的最后一个点一定是根节点. 由此,我们只一要确定对于每一个节点,它有多少个 ...
- HDU 1240——Asteroids!(三维BFS)POJ 2225——Asteroids
普通的三维广搜,须要注意的是输入:列,行,层 #include<iostream> #include<cstdio> #include<cstring> #incl ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- POJ 3422 Kaka's Matrix Travels
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9567 Accepted: ...
- poj 1390 Blocks
poj 1390 Blocks 题意 一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.问怎么消能使分数最大.. 题解 此题在徐源盛<对一类动态规划问题的研究&g ...
- HDU 3966 & POJ 3237 & HYSBZ 2243 树链剖分
树链剖分是一个很固定的套路 一般用来解决树上两点之间的路径更改与查询 思想是将一棵树分成不想交的几条链 并且由于dfs的顺序性 给每条链上的点或边标的号必定是连着的 那么每两个点之间的路径都可以拆成几 ...
- POJ 1459:Power Network(最大流)
http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...
随机推荐
- Wireshark漫谈(一)
可能有人会说,一个软件的安装有什么好谈的,无非就是"同意,同意,同意......是,是,是"诸如此类的选项.的确,Wireshark软件的安装步骤是挺简单的,不过本文不是想谈安装步 ...
- Microsoft Exchange本地和Exchange Online可以与第三方服务共享
很多人都知道Office 365中的Microsoft Exchange本地和Exchange Online可以与第三方服务共享您的个人数据?例如,在Exchange电子邮件中找到的任何地图地址都会发 ...
- Unity runtime性能分析器
一. Profiler: 1. CPU Usage A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间 B. Overhead: Profiler总体 ...
- java sql database相关收集
1 java prepareStatement http://www.importnew.com/5006.html 2 java ENGINE=InnoDB的使用 http://www.cnblog ...
- java ArrayList remove
packimport java.util.ArrayList;import java.util.List; public class ArrayListRemove { public static v ...
- Linux centos 6 配置php环境,扩展redis
1.首先安装一个虚拟机(我自己版本:VM 10.0.4) yum -y install openssl psmisc openssl-devel php-devel pcre-devel gcc gc ...
- Bootstrap历练实例:面板的标题
面板标题 我们可以通过以下两种方式来添加面板标题: 使用 .panel-heading class 可以很简单地向面板添加标题容器.to easily add a heading container ...
- MySQL创建数据库,用户,赋予权限
CREATE DATABASE 'voyager'; CREATE DATABASE `voyager`; CREATE USER 'dog'@'localhost' IDENTIFIED BY '1 ...
- php 常用函数集合(持续更新中...)
php 常用函数集合 在php的开发中,巧妙的运用php自带的一些函数,会起到事半功倍的效果,在此,主要记录一些常用的函数 1.time(),microtime()函数 time():获取当前时间戳 ...
- 【php】expose_php 作用
php.ini expose_php = On expose_php = Off