-
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