如果序列相同则输出YES,否则输出NO
2 567432 543267 576342 0
YES NO
这题我的思路是将序列转换成用数组表示的二叉树,然后比较两个数组是否相同即可。
源代码:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 10000;
int sTree[N];
int dTree[N];
void string2Tree(char *s, int *tree);
void insert(char c, int *tree);
bool isSameTree();
int main(){
	int n;
	while(cin>>n && n){
		memset(sTree, -1, sizeof(sTree));
		char s[1000], d[1000];
		cin >> s;
		string2Tree(s, sTree);
		for(int i=0; i<n; i++){
			cin >> d;
			if(strlen(s) != strlen(d)){
				cout << "NO" << endl;
				continue;
			}
			memset(dTree, -1, sizeof(dTree));
			string2Tree(d, dTree);
			if(isSameTree()) cout << "YES" << endl;
			else cout << "NO" << endl;
		}
	}
	return 0;
}
void string2Tree(char *s, int *tree){
	int len = strlen(s);
	tree[1] = s[0] - ‘0‘;
	for(int i=1; i<len; i++){
		insert(s[i], tree);
	}
}
void insert(char c, int *tree){
	int curPos = 1;
	int curChar = c - ‘0‘;
	while(tree[curPos] != -1){
		if(tree[curPos] < curChar){
			curPos = 2 * curPos + 1;
		}
		else{
			curPos = 2 * curPos;
		}
	}
	tree[curPos] = curChar;
}
bool isSameTree(){
	for(int i=1; i<N; i++){
		if(sTree[i] != dTree[i]) return false;
	}
	return true;
}
 
原文:http://www.cnblogs.com/chaos---/p/6528548.html