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

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

#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