HDU 3791

时间:2024-04-15 04:25:18

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[],ttree[]; char str[]; int main(){
int n;
while(~scanf("%d",&n),n){
memset(tree,-,sizeof(tree));
scanf("%s",str);
int len=strlen(str);
for(int i=;i<len;i++){
int a=str[i]-'';
int rt=;
while(tree[rt]!=-){
if(a<tree[rt])rt=lson;
else rt=rson;
}
tree[rt]=a;
}
for(int i=;i<n;i++){
scanf("%s",str);
len=strlen(str);
memset(ttree,-,sizeof(ttree));
for(int j=;j<len;j++){
int a=str[j]-'';
int rt=;
while(ttree[rt]!=-){
if(a<ttree[rt])rt=lson;
else rt=rson;
}
ttree[rt]=a;
}
int flag=;
for(int j=;j<;j++)
if(tree[j]!=ttree[j]){
flag=;
break;
}
if(flag)puts("YES");
else puts("NO");
}
}
return ;
}