Data Structure and algorithm problems determine whether the two sequences are in the same binary search tree Sequence

Source: Internet
Author: User
Description:
Determine whether the two sequences are of the same binary search tree Sequence
Input:
Start with N, (1 <= n <= 20) indicates there are n to be judged, and the input ends when n = 0.
The next row is a sequence with a length less than 10 and contains (0 ~ 9). There are 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. Determine whether the two sequences can form the same binary search tree.
Output:

If the sequence is the same, yes is output; otherwise no is output.

Sample input:
25674325432675763420
Sample output:
YESNO
Source:

A true Study on computer and software engineering of Zhejiang University in 2010


#include <iostream>#include <string>using namespace std;int a[1024];int b[1024];void createtree(string s, int c[]){int len = s.length();for (int i = 0; i < len; i++){int temp = s[i] - '0';for (int j = 1; j <= 1023;){if (c[j] == -1){c[j] = temp; break;}else if (c[j]>temp)j = j * 2;else{j = j * 2 + 1;}}}}int main(){int n, i;string s;string t;while (cin >> n&&n){for (i = 0; i < 1024; i++)a[i] = -1;cin>>s;createtree(s, a);while (n--){for (i = 0; i < 1024; i++)b[i] = -1;cin >> t;createtree(t, b);for (i = 0; i < 1024;i++)if (a[i] != b[i]) break;if (i == 1024)cout << "YES" << endl;elsecout << "NO" << endl;}}return 0;}



Data Structure and algorithm problems determine whether the two sequences are in the same binary search tree Sequence

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.