//数字节点输入顺序不同,构造的二叉搜索树也不一定相同
//前序遍历和中序遍历可以唯一确定一棵二叉树,而对二叉排序树而言,相同元素构成的的二叉排序树中序遍历一定相同。所以本题只要考察前序遍历或者后续遍历的二叉树的字符串是否相同就行
#include <iostream>
using namespace std;
string s[20];
struct TreeNode
{
char data;
TreeNode* leftchild;
TreeNode* rightchild;
TreeNode(char c)
{
data=c;
leftchild=NULL;
rightchild=NULL;
}
};
TreeNode* Insert(TreeNode* root,char x)
{
if(root==NULL)
{
root=new TreeNode(x);
}
else if(x<root->data)
{
root->leftchild=Insert(root->leftchild,x);
}
else {
root->rightchild=Insert(root->rightchild,x);
}
return root;
}
void PreOrder(TreeNode* root,string& str)//引用
{
if(root==NULL)return ;
str+=root->data;
PreOrder(root->leftchild,str);
PreOrder(root->rightchild,str);
}
int main() {
int n;
while (cin >>n) {
if(n==0)break;
for(int i=0;i<20;i++)
{
s[i]="";
}
string str="";
cin>>str;
TreeNode* root=NULL;
for(int i=0;i<str.size();i++)
{
root=Insert(root,str[i]);
}
string rootstr="";
PreOrder(root,rootstr);//引用
for(int i=0;i<n;i++)
{
cin>>s[i];
TreeNode* root1=NULL;
for(int j=0;j<s[i].size();j++)
{
root1=Insert(root1,s[i][j]);//注意细节s[i][j]
}
string root1str="";
PreOrder(root1,root1str);//引用
if(root1str==rootstr)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
}