HDU 3791

时间:2014-11-05 19:03:53   收藏:0   阅读:270

http://acm.hdu.edu.cn/showproblem.php?pid=3791

建立二叉树,对比是否相同

bubuko.com,布布扣
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

#define lson rt<<1
#define rson rt<<1|1

int tree[2500],ttree[2500];

char str[25];

int main(){
    int n;
    while(~scanf("%d",&n),n){
        memset(tree,-1,sizeof(tree));
        scanf("%s",str);
        int len=strlen(str);
        for(int i=0;i<len;i++){
            int a=str[i]-0;
            int rt=1;
            while(tree[rt]!=-1){
                if(a<tree[rt])rt=lson;
                else rt=rson;
            }
            tree[rt]=a;
        }
        for(int i=0;i<n;i++){
            scanf("%s",str);
            len=strlen(str);
            memset(ttree,-1,sizeof(ttree));
            for(int j=0;j<len;j++){
                int a=str[j]-0;
                int rt=1;
                while(ttree[rt]!=-1){
                    if(a<ttree[rt])rt=lson;
                    else rt=rson;
                }
                ttree[rt]=a;
            }
            int flag=1;
            for(int j=1;j<2500;j++)
                if(tree[j]!=ttree[j]){
                    flag=0;
                    break;
                }
            if(flag)puts("YES");
            else puts("NO");
        }
    }
    return 0;
}
View Code

 

原文:http://www.cnblogs.com/xiaohongmao/p/4076809.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!