Jizhong Life 1.5

topic:

Among the many data structures, binary tree is a special and important structure, with a wide range of applications. Or a binary tree nodes, or the root has one and only one node of the binary bits, the remaining nodes are divided into two disjoint subsets, one subset as the left, the right and the other as a subset. Each subset is a binary tree.
Traversing a binary search is tapped on a tour path where each node so that each node are visited once, and is accessed only once. There are three most common way to navigate:
(1) preorder traversal: If the binary tree is empty, the air operation; otherwise, the first visit the root node, traversing the left subtree order before then, the right sub-tree traversal sequence before the final.
(2) preorder: If the binary tree is empty, the empty; otherwise preorder first left subtree, then visit the root node, and finally preorder right subtree.
(3) order traversal: If the binary tree is empty, the air operation; otherwise preorder traversal has left subtree, then the right subtree order traversal, and finally access the root node.

 

 

For example binary tree before the sequence shown in FIG. 1 is traversing ABCD, sequence preorder is CBAD, postorder traversal order is CBDA.
Of a binary tree, if given before the preorder traversal order and node access order, then the order postorder traversal is uniquely determined, it is also very easy to find. But if we only know the order of the preamble and post-order traversal of traversal, inorder traversal order is uncertain, for example: preorder traversal sequence is ABCD, then traversing order is CBDA, then there are two binary trees meet this sequence, see Figures 1 and 2.
The question now is before the given order and post-order traversal order traversal, requiring a total number of trees of different forms of binary tree traversal order to meet this.

Input:

Entire input two lines, the first line gives access sequence preorder front, rear access sequence preorder given a second row.

Binary tree nodes represented by a capital letter, superscript letters do not have the same two nodes. Input data does not contain spaces, and ensure that at least meet the requirements of a binary tree.

Output:

Output an integer, meet the requirements for the binary number of different forms.
 

Sample input:

ABCD 
CBDA
Sample output:
2

Program:

Method 1: 
violence binary tree structure, meets the requirements of the judgment. 
Expected Score: 10

 

Method 2: 
direct output Sample 
Score: 30

 

Correct answer:

When we look at the follow-up to traverse upside down, the order becomes a right-left roots.

When two nodes are connected together and there is a leaf node, which is the same in the two traversal.

Adjacency matrix can be optimized by: space for time.

In fact, you can not optimize

code:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 long long power(int x) {
 6     long long t=1;
 7     for (int i=0; i<x; i++) {
 8         t*=2;
 9     }
10     return t;
11 }
12 int main() {
13     //freopen("tree.in", "r", stdin);
14     //freopen("tree.out", "w", stdout);
15     char fir[100000], sec[100000];
16     cin>>fir>>sec;
17     int len=strlen(fir);
18     if (len==1) cout<<1;
19     else {
20         int sum=0;
21         for (int i=0; i<len-1; i++) {
22             for (int j=len-1; j>0; j--) {
23                 if (fir[i]==sec[j] && fir[i+1]==sec[j-1]) {
24                     sum++;
25                     break;
26                 }
27             }
28         }
29         cout<<power(sum);
30     }
31     return 0;
32 }

 

Guess you like

Origin www.cnblogs.com/ouzijun-OJ/p/11389270.html