码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3791 二叉搜索树

时间:2015-04-19 10:06:43      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

简单的二叉搜索树建树过程

#include<stdio.h>
#include<string.h>
int tree1[1500], tree2[1500];
char s[1000], k[1000];
void build1()
{
    int i;
    tree1[1] = s[0] - 0;
    int y = strlen(s);
    for (i = 1; i < y; i++)
    {
        int now = 1;
        while (1)
        {
            if (tree1[now] == -1) break;
            else
            {
                if (tree1[now] < s[i] - 0)now = now * 2 + 1;
                else now = now * 2;
            }
        }
        tree1[now] = s[i] - 0;
    }
}
void build2()
{
    int i;
    tree2[1] = k[0] - 0;
    int y = strlen(k);
    for (i = 1; i < y; i++)
    {
        int now = 1;
        while (1)
        {
            if (tree2[now] == -1) break;
            else
            {
                if (tree2[now] < k[i] - 0) now = now * 2 + 1;
                else now = now * 2;
            }
        }
        tree2[now] = k[i] - 0;
    }
}
int main()
{
    int n, i, j, g;
    while (~scanf("%d", &n))
    {
        if (n == 0) break;
        scanf("%s", s);

        for (i = 0; i < 1350; i++) tree1[i] = -1;
        build1();
        for (i = 0; i < n; i++)
        {
            for (g = 0; g < 1350; g++) tree2[g] = -1;
            scanf("%s", k);
            build2();
            for (j = 1; j < 1400; j++)
            {
                if (tree1[j] != tree2[j]) break;
            }
            if (j == 1400) puts("YES");
            else puts("NO");
        }
    }
    return 0;
}

 

hdu 3791 二叉搜索树

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/4438626.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!