P1229 Traversal Problem

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: ![](https://cdn.luogu.com.cn/upload/image_hosting/w75s9yip.png) 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 $s_1$ of the binary tree, and the second line is the postorder traversal $s_2$. It is guaranteed that at least one binary tree satisfies the given information. $s_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 $2^{63}-1$.

Explanation/Hint

Translated by ChatGPT 5