#P1229. 遍历问题

遍历问题

Description

We are all familiar with preorder, inorder, and postorder traversals of binary trees. In data structures, a common problem is: given the preorder and inorder traversals of a binary tree, find its postorder traversal; similarly, given the postorder and inorder traversals, you can find its preorder traversal. However, given the preorder and postorder traversals of a binary tree, you cannot determine its inorder traversal. Consider the binary trees in the figure below:

All these binary trees have the same preorder and postorder traversals, but different inorder traversals.

Input Format

Two lines in total. The first line is the preorder traversal s1s_1 of the binary tree, and the second line is the postorder traversal s2s_2.

It is guaranteed that at least one binary tree satisfies the given information. s1,s2s_1, s_2 contain only lowercase letters, and within each string no letter appears more than once.

Output Format

Output the total number of possible inorder traversal sequences. The result does not exceed 26312^{63}-1.

abc                           
cba

4

Hint

Translated by ChatGPT 5