POJ1240 m叉树
Pre-Post-erous!
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 1915 | Accepted: 1158 | 
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
题意:已知前序遍历和后序遍历,求中序遍历的种数。(如果已知先序和中序,那么后序唯一,如果已知中序和后序,先序也是唯一的)
思路:。。。。。。
 View Code
View Code 
1 #include<stdio.h> 2 #include<string.h> 3 #include <stdio.h> 4 #include <string.h> 5 int N, M; 6 char s1[30],s2[30]; 7 int C(int n, int k) //计算组合数C(n, k) 8 { 9 int i,res=1; 10 if(k>n/2) k=n-k; 11 for(i=1;i<=k;++i) 12 { 13 res=res*(n-i+1)/i; 14 } 15 return res; 16 } 17 int DP(int m,char *pre,char *post) 18 { 19 int left=1,right=0,nchild=0,res=1;//res: 结果 20 //left: pre的指针,指向子树根节点。根节点不用看了,所以从1开始 21 //right: post的指针,指向子树根节点 22 //nchild: 有多少个子树 23 24 while(left<m) //枚举每棵子树 25 { 26 nchild++; //多一棵子树 27 while(pre[left]!=post[right]) 28 {right++;} 29 res*=DP((right+1)-left+1,pre+left,post+left-1); 30 //对每棵子树递归进行动态规划,由乘法原理,应该是把结果乘起来 31 left=right+2; //设置寻找下一棵子树 32 } 33 return res*C(N,nchild);//因为是m叉树 34 } 35 int main() 36 { 37 while(scanf("%d",&N),N) 38 { 39 scanf("%s%s",s1,s2); 40 M=strlen(s1);//结点数 41 printf("%d\n",DP(M,s1,s2));//递归求解 42 } 43 return 0; 44 }
 
                     
                    
                 
                    
                

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号