/************************************************* ************************************************** *******************
Given an insertion sequence, a binary search tree can be uniquely determined. However, a given binary search tree can be obtained from many different insertion sequences. For example, insert an initially empty binary search tree according to the sequence {2, 1, 3} and {2, 3, 1}, and get the same result. So for the various input sequences, you need to judge whether they can generate the same binary search tree.
Input format: The
input contains several sets of test data. The first row of each group of data gives two positive integers N (≤10) and L, which are the number of inserted elements in each sequence and the number of sequences to be checked. The second line gives N positive integers separated by spaces as the initial insertion sequence. In the last L rows, each row gives N inserted elements, which belong to L sequences to be checked.
For simplicity, we ensure that each insertion sequence is a permutation from 1 to N. When it is read that N is 0, the input of the mark is over, and this group of data should not be processed.
Output format:
For each sequence to be checked, if the binary search tree generated is the same as the corresponding initial sequence, output "Yes", otherwise output "No".
Input example:
4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0
Output example:
Yes
No
No
****************** ************************************************** ************************************************** /
// root node to the first number, all larger than the root node as the right subtree sequence number, otherwise, the left subtree sequence
// "extraction" All right subtree nodes, to give: Sentinel -> Right subtree, the original sequence is left at this time: root node -> left subtree
// recursively sort the right and left subtrees, connect to the root node, release the sentinel, and
finally get the preorder traversal
//Judging whether the same search tree is based on the sequence of the preorder traversal (the preorder traversal determines the search tree)
#include <iostream>
using namespace std;
typedef struct TNode* Tree;
typedef struct TNode {
int Data;
Tree Next;
};
Tree InputOrder(int N)
{
int X;
Tree root, tmp, input;
root = new struct TNode({
0, NULL});
tmp = root;
for (int n(0); n < N; n++)
{
cin >> X;
input = new struct TNode({
X, NULL });
tmp->Next = input;
tmp = input;
}
tmp = root;
root = root->Next;
delete tmp;
return root;
}
Tree PreOrder(Tree T)
{
//以第一个数为根结点,所有比根结点大的数作为右子树序列,否则为左子树序列
//“抽取”所有右子树结点,得到:哨兵->右子树,此时原序列剩下:根结点->左子树
//对右子树、左子树递归排序,续接到根结点,释放哨兵
//最终得到序列的先序遍历
Tree Root(T);
if (T) {
Tree Right = new struct TNode({
T->Data, NULL }), RightRoot(Right);
while(T->Next)
{
if (T->Next->Data > Root->Data){
Right->Next = T->Next;
T->Next = T->Next->Next;
Right = Right->Next;
}
else
T = T->Next;
}
Right->Next = NULL; //尾部指向NULL
Right = RightRoot->Next; //“抽取”右子树
delete RightRoot; //释放哨兵
Tree Left(Root->Next);
Root->Next = PreOrder(Left); //对左子树递归排序,根结点指向左子树
for (Left = Root; Left->Next; Left = Left->Next); //移动到左子树尾部
Left->Next = PreOrder(Right); //对右子树递归排序,左子树尾部指向右子树
}
return Root;
}
bool Compare(Tree T1, Tree T2)
{
while (T1 && T2 && T1->Data == T2->Data)
{
T1 = T1->Next;
T2 = T2->Next;
}
return !(T1 || T2); //若T1, T2均为空,则所有结点均相等
}
int main()
{
int N, L;
cin >> N;
while (N) {
cin >> L;
Tree RefOrder = InputOrder(N);
RefOrder = PreOrder(RefOrder);
while (L--) {
Tree CmpOrder = InputOrder(N);
CmpOrder = PreOrder(CmpOrder);
if (Compare(RefOrder, CmpOrder))
cout << "Yes\n";
else
cout << "No\n";
}
cin >> N;
}
return 0;
}