Pre-Post (已知先序和后序遍历的m叉树有几种)

6 篇文章 0 订阅
时间限制:1秒  空间限制:65536K  热度指数:173
 算法知识视频讲解

题目描述

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.
示例1

输入

2 abc cba
2 abc bca
10 abc bca
13 abejkcfghid jkebfghicda

输出

4
1
45
207352860   

用递归来做,排列组合的数学公式

//#include "stdafx.h"
#include"stdio.h"
#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
const int maxn=15;
int m;
ll c[21];
string s1,s2;
void init(){
   c[0]=1;
   for(int i=1;i<=20;i++)
	   c[i]=c[i-1]*i;
}


ll cal(int n){
	return c[m]/(c[m-n]*c[n]);



}


ll func(string a,string b){//a[0]和b[b.length()-1]是一样的
	a=a.substr(1,a.length());
	b=b.substr(0,b.length()-1);
	if(a==b)return cal(a.length());//如果a和b相等,那么其字符一定是在同一层上
	int j=0;int n=0;
	ll ans=1;
	for(int i=0;j<a.length()&&i<b.length();i++){
			if(a[j]==b[i]){
				ans*=func(a.substr(j,i-j+1),b.substr(j,i-j+1));//分割成各个子树
				j=i+1;n++;}//n统计子树的个数
		    
		}
	     ans*=cal(n);
		
	 return ans;
	


}


int main(){

//	freopen("c://jin.txt","r",stdin);
	init();
	while(cin>>m){
		if(m==0)break;
	        cin>>s1>>s2;
	        cout<<func(s1,s2)<<endl;
		}


	//freopen("CON","r",stdin);
	//system("pause");

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值