• hdu 3791 二叉搜索树


    原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20911

    简单题如下:

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 using std::vector;
     8 const int Max_N = 100;
     9 struct Node{
    10     int v;
    11     Node *ch[2];
    12     inline void set(int _v = 0, Node *p = NULL){
    13         v = _v;
    14         ch[0] = ch[1] = p;
    15     }
    16 };
    17 struct BinaryTree{
    18     Node *tail, *root, *null;
    19     Node stack[Max_N];
    20     void init(){
    21         tail = &stack[0];
    22         null = tail++;
    23         null->set();
    24         root = null;
    25     }
    26     inline  Node *newNode(int v){
    27         Node *p = tail++;
    28         p->set(v, null);
    29         return p;
    30     }
    31     inline void insert(Node* &x, int v){
    32         if (x == null){
    33             x = newNode(v);
    34             return;
    35         }
    36         insert(x->ch[v > x->v], v);
    37     }
    38     inline void dfs(Node *x, vector<int> &ans){
    39         if (x != null){
    40             ans.push_back(x->v);
    41             dfs(x->ch[0], ans);
    42             dfs(x->ch[1], ans);
    43         }
    44     }
    45     inline void insert(int v){
    46         insert(root, v);
    47     }
    48     inline void dfs(vector<int> &ans){
    49         dfs(root, ans);
    50     }
    51     inline void gogo(vector<int> &ans){
    52         int m;
    53         char buf[100]; 
    54         while (getchar() != '
    ');
    55         scanf("%s", buf);
    56         init(), m = strlen(buf);
    57         for (int i = 0; i < m; i++) insert(buf[i] - '0');
    58         dfs(ans);
    59     }
    60 }tree;
    61 int main(){
    62 #ifdef LOCAL
    63     freopen("in.txt", "r", stdin);
    64     freopen("out.txt", "w+", stdout);
    65 #endif
    66     int n;
    67     while (~scanf("%d", &n) && n){
    68         vector<int> a, b;
    69         tree.gogo(a);
    70         for (int i = 0; i < n; i++){
    71             tree.gogo(b);
    72             if (a == b) puts("YES");
    73             else puts("NO");
    74             b.clear();
    75         }
    76     }
    77     return 0;
    78 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    反射、面向对象(基础篇)
    配置文件
    模块补充 、迭代器和 生成器
    模块
    正则表达式
    冒泡排序、递归和简单装饰器
    使用Apache Archiva管理Maven仓库
    AppScan9.0.3.5漏洞扫描记录
    Linux环境下安装Websphere8.5.5
    如何排查网络通讯状况
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4479633.html
Copyright © 2020-2023  润新知