Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2148 Accepted Submission(s): 957
node
#include <stdio.h> #include <iostream> #include <string.h> #include <string.h> using namespace std; typedef struct node { char data; node *lchild; node *rchild; node() { lchild = rchild = NULL; } }TreeNode; void CreateTree(TreeNode *&pRoot, char ch) { if (pRoot == NULL) { pRoot = new TreeNode; pRoot->data = ch; } else { if (ch > pRoot->data) { CreateTree(pRoot->rchild, ch); } else { CreateTree(pRoot->lchild, ch); } } } bool IsEqual(TreeNode *pRoot1, TreeNode *pRoot2) { if (pRoot1 != NULL && pRoot2 != NULL) { if (pRoot1->data != pRoot2->data) { return false; } if (IsEqual(pRoot1->lchild, pRoot2->lchild) && IsEqual(pRoot1->rchild, pRoot2->rchild)) { return true; } } else { if (pRoot1 == NULL && pRoot2 == NULL) { return true; } } return false; } void DeleteTree(TreeNode *pRoot) { if (pRoot != NULL) { DeleteTree(pRoot->lchild); DeleteTree(pRoot->rchild); } delete pRoot; } int main() { int n; char str[15]; while(scanf("%d", &n) != EOF && n != 0) { scanf("%s", str); TreeNode *pRoot = NULL; for (int i = 0; i < strlen(str); i++) { CreateTree(pRoot, str[i]); } for (int i = 0; i < n; i++) { scanf("%s", str); TreeNode *pRoot1 = NULL; for (int j = 0; j < strlen(str); j++) { CreateTree(pRoot1, str[j]); } if (IsEqual(pRoot, pRoot1)) { printf("YES\n"); } else { printf("NO\n"); } DeleteTree(pRoot1); } DeleteTree(pRoot); } return 0; }