思路就是先把标准树建立起来,然后循环n次,每次在建一棵树,和标准树对比就可以了,代码中有注释的哦!
#include <iostream>
#include<string.h>
using namespace std;
struct tree
{
tree *l,*r;//左树和右树
int data;//数据
};
tree* ru(tree *t,int x)//建树
{
if(t==NULL)//根
{
t=new tree;
t->l=NULL;
t->r=NULL;
t->data=x;
}
else
{
if(t->data>x)
{
t->l=ru(t->l,x);
}
else t->r=ru(t->r,x);
}
return t;
}
int show(tree *t1,tree *t2)//对比两棵树
{
if(t1==NULL&&t2==NULL)return 1;
if(t1&&t2)
{
if(t1->data!=t2->data)return 0;
else if((show(t1->l,t2->l))&&show(t1->r,t2->r))return 1;
}
return 0;
}
int main()
{
int n,i;
char a[15];
//这个建树和while里面的建树一样
tree *tre1=new tree;
tre1=NULL;
cin>>a;
int l=strlen(a);
for(i=0; i<l; i++)
{
tre1=ru(tre1,a[i]-'0');
}
cin>>n;
while(n--)
{
//建立查询的树
cin>>a;
l=strlen(a);
tree *tre2=new tree;
tre2=NULL;
for(i=0; i<l; i++)
{
tre2=ru(tre2,a[i]-'0');
}
i=show(tre1,tre2);//对比两树是否相等
if(i==1)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}