zoukankan      html  css  js  c++  java
  • Elven Postman---hdu5444(二叉树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5444

     有一个序列,由这个序列可以画出一颗二叉树(每个节点的左边(W)都比它大,右边(E)都比它小),我们每次从树根出发每次向左向右的找到对应的点,求这个过程是怎样的用WE表示;

     

    最近刚好看到了数据结构,可以用用指针做

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #include<vector>
    using namespace std;
    #define N 1100
    #define INF 0x3f3f3f3f
    #define met(a) memset(a, 0, sizeof(a))
    
    struct BST
    {
        int x;
        BST *L, *R;
        BST(int m=0):x(m)
        {
            L=NULL;
            R=NULL;
        };
    }a[N], *r;
    
    vector<char> path[N];
    void Inset(int x)
    {
        BST *t = r;
    
        while(t!=NULL)
        {
            if(x > t->x)
            {
                path[x].push_back('W');
                if(t->R == NULL)
                {
                    t->R = new BST(x);
                    break;
                }
                else
                    t=t->R;
            }
            else
            {
                path[x].push_back('E');
                if(t->L == NULL)
                {
                    t->L=new BST(x);
                    break;
                }
                else
                    t=t->L;
            }
        }
    }
    int main()
    {
        int T, n, q, x;
        scanf("%d", &T);
        while(T--)
        {
            met(a);
            for(int i=0; i<N; i++)
                path[i].clear();
            scanf("%d", &n);
            scanf("%d", &x);
            r=new BST(x);
            for(int i=2; i<=n; i++)
            {
                scanf("%d", &x);
                Inset(x);
            }
            scanf("%d", &q);
            while(q--)
            {
                scanf("%d", &x);
                int len=path[x].size();
                for(int i=0; i<len; i++)
                    printf("%c", path[x][i]);
                printf("
    ");
            }
        }
        return 0;
    }
    View Code

     后来发现由于大小的关系我们可以直接找;

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <stack>
    #include <math.h>
    
    using namespace std;
    
    #define met(a, b) memset(a, b, sizeof(a))
    #define N 10003
    #define INF 0x3f3f3f3f
    
    typedef long long LL;
    
    int a[N], n;
    
    void solve(int x)
    {
        char s[N];
        met(s, 0);
        int k = 0, Max = 0, Min = INF;
        for(int i=1; i<=n; i++)
        {
            if(x < a[i] && a[i] < Min)
            ///当这个点比需要找的点小,并且小于之前遇到的最小的那个说明我们要沿着它的E方向找;
            {
                s[k++] = 'E';
                Min = a[i];
            }
            else if(x > a[i] && a[i] > Max)
            ///当这个点比需要找的点大,并且大于之前遇到的最大的那个说明我们要沿着它的W方向找;
            {
                s[k++] = 'W';
                Max = a[i];
            }
            else if(x == a[i])///当找到时输出并结束;
            {
                printf("%s
    ", s);
                return;
            }
        }
    }
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            met(a, 0);
    
            scanf("%d", &n);
            for(int i=1; i<=n; i++)
                scanf("%d", &a[i]);
    
            int m;
    
            scanf("%d", &m);
            for(int i=1; i<=m; i++)
            {
                int x;
                scanf("%d", &x);
                solve(x);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    angular 个人零点学习
    angularjs 五大关键点
    OA项目学习总结
    oa
    时间插件
    angular js模态框
    搜索
    xianduanshu
    o-o
    paibingbuzhen
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4812349.html
Copyright © 2011-2022 走看看