Problem solving of HDU 3791 Binary Search Tree

Source: Internet
Author: User

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 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.