2 567432 543267 576342 0
YES NO
一条浙大考研上机题目。考二叉树构建。
直接使用静态数组过了,由于数据不大。
#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, a, node<<1|1); else constrctTree(T, a, (node+1)<<1); } int main() { int n; secTree[1<<9] = '\0'; Tree[1<<9] = '\0'; while (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; }
HDU 3791 二叉搜索树 题解,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/hrhguanli/p/3833648.html