Pre-Post-erous!
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2356 Accepted: 1450
Description
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:
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
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.
Output
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.
Sample Input
2 abc cba
2 abc bca
10 abc bca
13 abejkcfghid jkebfghicda
0
Sample Output
4
1
45
207352860
Source
East Central North America 2002
题意很简单 给出一颗n叉树的前序遍历和后序遍历 求树的可能形状的数量
pre[0-len]表示为子树的前序遍历
post[0-len]为子树的后序遍历
pre[0]必定为树根
pre[1]必定为树根的最左边的儿子
令j=pre[1]在post(后序遍历)中的下标
则post[0,j]就是以post[j]为根的子树后序遍历
pre[1,1+j-sPost]就是前序遍历
pre[1+j-sPost+1]就是树根pre[0]的第2个儿子的根节点
递归下去 统计每棵树的树根有几个儿子
C(儿子个数,n)儿子1的形状个数儿子2的形状个数*….儿子n的形状个数
就是这棵树的形状个数
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
char pre[30];
char post[30];
inline int C(int m,int n){
if(m==0)
return 1;
if(n==m)
return 1;
return C(m,n-1)+C(m-1,n-1);
}
int f(int sPre,int ePre,int sPost,int ePost,int n){
int root=pre[sPre];
int numOfChild=0;//以root为根 有几个儿子
int ans=1;
int tmp=sPost;
for(int i=sPre+1;i<=ePre;){
char child=pre[i];
++numOfChild;
int len=0,j;
for(j=sPost;j<=ePost;++j){
if(post[j]==child){
len=j-tmp+1;//这颗子树的长度or节点个数
ans*=f(i,i+len-1,tmp,j,n);
tmp=j+1;//记录下一个儿子为根的子树从哪个下标开始
break;
}
}
i+=len;//下一个儿子的下标
}
return ans*C(numOfChild,n);
}
int main()
{
//freopen("/home/lu/文档/r.txt","r",stdin);
//freopen("/home/lu/文档/w.txt","w",stdout);
int n;
while(~scanf("%d%s%s",&n,pre,post),n){
int len=strlen(pre);
printf("%d\n",f(0,len-1,0,len-1,n));
}
return 0;
}