The case of the M-fork Tree First order and post sequence

Source: Internet
Author: User

Title Description:

We is all familiar with pre-order, In-order and post-order traversals of binary trees. A common problem in data structure classes are 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 cannot determine the in-order traversal of a tree when given its pre-order and Post-order Traversa Ls. Consider the four binary trees below:



All of these trees has the same pre-order and Post-order traversals. This phenomenon isn't restricted to binary trees, but holds for general m-ary trees as well.

Input:

        input would consist of mul Tiple problem instances. Each instance would consist of a line of the form
m s1 s2
        indicating tha t the trees is m-ary trees, S1 is the pre-order traversal and S2 is the Post-order traversal. All traversal strings would consist of lowercase alphabetic characters. For all input instances, 1 <= m <= and the length of S1 and S2 would be between 1 and 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 would be u Sed in the strings. An input line of 0 would terminate the input.

Output:
For each problem instance, you should output one line containing the number of possible trees which would result in the PR E-order and Post-order traversals for the instance. All output values would be within the range of a 32-bit signed integer. For each problem instance, you be guaranteed that there was at least one tree with the given pre-order and Post-order Trav Ersals.
Sample input:
2 ABC cba2 ABC bca10 ABC bca13 abejkcfghid JKEBFGHICDA
Sample output:
4145207352860
Classic Code:
#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; }Explanation of Baidu Library: Http://wenku.baidu.com/link?url=ZddYeW-pYEgst83coqElNsI-aHY_ Jwyuhwskbhkrxpnwxycmcn0ltdqq7k-igdzkr48wdgg4chriks1h5cwunhzpqbjbl3a4n_oluffbe4i

The case of the M-fork Tree First order and post sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.