22544_Pre-Post-erous!

2022-5-16 18:22| 发布者: Hocassian| 查看: 18| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-5-89506545416300-Problem List-采集的数据-后羿采集器.html

Pro.ID

22544

Title

Pre-Post-erous!

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=22544

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 1200/400 MS (Java/Others)     Memory Limit: 131072/65536 K (Java/Others)
  • 描述

    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.

    输出

    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:

    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

    样例输入

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

    样例输出

    4

    1
    45
    207352860

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部