hdu 3791

建树

直接使用数组会更简单


#include "stdio.h"
#include "string.h"
#include "stdlib.h"

typedef struct _Node{
	int n;
	struct _Node* lf;
	struct _Node* rt;
}Node, *pNode;

int cmp(pNode a, pNode b){
	if(!a || !b){
		if(a==b)
			return 1;
		else
			return 0;
	}
	if(a->n != b->n) return 0;
	if(!cmp(a->lf, b->lf))
		return 0;
	if(!cmp(a->rt, b->rt))
		return 0;
	return 1;
}

void insert(pNode h, int e){
	if(e<h->n){
		if(h->lf == 0){
			h->lf = (pNode)malloc(sizeof(Node));
			h->lf->n = e;
			h->lf->lf = h->lf->rt = 0;
		}else
			insert(h->lf, e);
	}
	else{
		if(h->rt == 0){
			h->rt = (pNode)malloc(sizeof(Node));
			h->rt->n = e;
			h->rt->lf = h->rt->rt = 0;
		}else
			insert(h->rt, e);
	}
}

void del(pNode h){
	if(!h) return;
	del(h->lf);
	del(h->rt);
	free(h);
}

void main(){
	int i, j;
	int n, l, ll;
	char seq[11];
	Node src, tgt;

	freopen("in.txt", "r", stdin);
	while(scanf("%d", &n), n){
		getchar();
		gets(seq);
		l = strlen(seq);
		src.n = seq[0]-48;
		src.lf = src.rt = 0;
		for(i=1; i<l; i++) 
			insert(&src, seq[i]-48);

		for(i=0; i<n; i++){
			gets(seq);
			ll = strlen(seq);
			if(ll != l)
				break;
			tgt.n = seq[0]-48;
			tgt.lf = tgt.rt = 0;
			for(j=1; j<ll; j++) 
				insert(&tgt, seq[j]-48);

			if(cmp(&src, &tgt))
				printf("YES");
			else
				printf("NO");
			printf("\n");
			del(tgt.lf);
			del(tgt.rt);
		}
		del(src.lf);
		del(src.rt);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值