hdu 3791

来源:互联网 发布:软件和信息技术服务 编辑:程序博客网 时间:2024/06/10 22:54

建树

直接使用数组会更简单


#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;elsereturn 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;}elseinsert(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;}elseinsert(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");elseprintf("NO");printf("\n");del(tgt.lf);del(tgt.rt);}del(src.lf);del(src.rt);}}