Problem description: determines whether the two sequences are the same binary search tree sequence.
Input starts with N, (1 <= n <= 20) indicates that N must be inferred, and n = 0 indicates that the input ends.
The next row is a sequence with a length less than 10, including (0 ~ 9) No repeated numbers. Based on this sequence, a binary search tree can be constructed.
The next n rows have N sequences. The format of each sequence is the same as that of the first sequence. Please infer whether these two sequences can form the same binary search tree.
If the output sequence is the same, yes is output; otherwise, no is output.
Sample Input
25674325432675763420
Sample output
Yesno
A question about the master of Zhejiang University. Test binary tree construction.
The static array is used directly because the data is not large.
# Include <stdio. h >#include <iostream >#include <string> Using STD: string; Using STD: CIN; const int size = (1 <9) + 1; char tree [size]; char sectree [size]; string root, child; void constrcttree (char T [], char a, int node = 0) {If (T [node] = 'B') {T [node] = A; return;} if (a <t [node]) constrcttree (T,, node <1 | 1); else constrcttree (T, A, (node + 1) <1) ;}int main () {int N; sectree [1 <9] = '\ 0'; tree [1 <9] =' \ 0'; Wh Ile (scanf ("% d", & N) & n! = 0) {for (INT I = 0; I <1 <9; I ++) tree [I] = 'B'; CIN> root; for (INT I = 0; I <(INT) root. size (); I ++) {constrcttree (tree, root [I]) ;}for (INT I = 0; I <n; I ++) {CIN> child; if (root. size ()! = Child. size () puts ("no"); else {for (Int J = 0; j <1 <9; j ++) sectree [J] = 'B '; for (INT I = 0; I <(INT) child. size (); I ++) {constrcttree (sectree, child [I]);} If (strcmp (tree, sectree) = 0) puts ("yes "); else puts ("no") ;}} return 0 ;}