Codevs 1029 times the question of the calendartime limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle Description
Description
We are all familiar with the binary tree pre-order, the middle sequence, post-order traversal, in the data structure often put forward such a problem: known as a binary tree of the pre-sequence and the middle sequence traversal, to its post-order traversal, the corresponding, known as a binary tree of the post-order traversal and sequence traversal sequences you can also find its pre- However, given the preamble and order of a binary tree, you cannot determine which sequence traversal sequence, consider a few binary trees:
All of these binary trees have the same pre-sequence traversal and post-order traversal, but the sequence traversal is not the same.
Enter a description
Input Description
The input file total 2 lines, the first line represents the previous sequence traversal results of the tree, the second row represents the tree's post-order traversal results. The character set entered is {A-Z} and does not exceed 26 in length.
Output description
Output Description
The output file contains only an integer that does not exceed the long integer, representing the total number of possible sequential traversal sequences.
Sample input
Sample Input
Abc
Cba
Sample output
Sample Output
4
1 /*Although the topic is to use a tree-shaped DP or search, but if the structure of the tree deep enough to understand, you can do it directly. 2 It is obvious that the probability of a binary tree is only related to the position of the leaf node without the sibling node when it is known that the sequence traversal and the post-order traversal of the two-fork trees are determined. 3 assuming that there are n of leaf nodes without a sibling node, there are 2^n on the likelihood side. (It doesn't prove it)4 So, how do you calculate the value of n? 5 here is a property:6 A binary tree's pre-sequence traversal a1a2a3...ai and post-order traversal B1b2b3...bi have a relationship:7 The root of a leaf node without a sibling node is labeled I in a sequence, labeled J in the B sequence .8 then there is a[i-1] = = B[j+1]9 this is because when the root has only one subtrees tree, the preamble and post-order traversal are the children who first traverse it, and are the only child, so the relative position is the same. Ten */ One#include <iostream> A using namespacestd; - stringPre,hou; - intLenpre,lenhou; the intans=0; - intMain () - { -Cin>>pre>>Hou; +Lenpre=pre.length ()-1; -Lenhou=hou.length ()-1; + for(intI=1; i<=lenpre;++i) A for(intj=0; j<=lenhou-1;++j) at if(pre[i]==hou[j]&&pre[i-1]==hou[j+1]) -ans++; -cout<< (Long Long)(1<<ans) <<Endl; - return 0; -}
The problem of binary tree structure Codevs 1029 times