树的同构
试题描述
给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。
输入
输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出“-”。给出的数据间用一个空格分隔。注意:题目保证每个结点中存储的字母是不同的。
输出
如果两棵树是同构的,输出“Yes”,否则输出“No”。
样例输入
8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -
样例输出
Yes
#include<stdio.h>
#include<stdlib.h>
#define Null -1
struct TreeNode{
char Element;
int Left;
int Right;
}T1[10],T2[10];
int n;
int check[10];
int BuildTree(struct TreeNode T[])
{
int Root=Null;
int i;
char cl,cr;
scanf("%d\n",&n);
for(i=0;i<n;i++) check[i]=0;
for(i=0;i<n;i++)
{
scanf("%c %c %c\n",&T[i].Element,&cl,&cr);
if(cl!='-'){
T[i].Left=cl-'0';
check[T[i].Left]=1;
}
else T[i].Left=Null;
if(cr!='-'){
T[i].Right=cr-'0';
check[T[i].Right]=1;
}
else T[i].Right=Null;
}
for(i=0;i<n;i++)
if(!check[i]) break;
Root=i;
return Root;
}
int isomerphic(int R1,int R2)
{
if((R1==-1)&&(R2==-1)) return 1;
if(((R1==-1)&&(R2!=-1))||((R1!=-1)&&(R2==-1))) return 0;
if(T1[R1].Element!=T2[R2].Element) return 0;
if((T1[R1].Left==-1)&&(T2[R2].Left==-1))
return isomerphic(T1[R1].Right,T2[R2].Right);
if(((T1[R1].Left!=-1)&&(T2[R2].Left!=-1))&&((T1[T1[R1].Left].Element)==(T2[T2[R2].Left].Element)))
return (isomerphic(T1[R1].Left,T2[R2].Left)&&isomerphic(T1[R1].Right,T2[R2].Right));
else
return (isomerphic(T1[R1].Left,T2[R2].Right)&&isomerphic(T1[R1].Right,T2[R2].Left));
}
int main()
{
int R1,R2;
R1=BuildTree(T1);
R2=BuildTree(T2);
if(isomerphic(R1,R2)) printf("Yes\n");
else printf("No\n");
return 0;
}
试题描述
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.
输入
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a “-” will be put at the position. Any pair of children are separated by a space.
输出
For each test case, print in one line all the leaves" indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
样例输入
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
样例输出
4 1 5
#include <stdio.h>
#include <stdlib.h>
#define MaxTree 10
#define Tree int
#define ElementType int
#define Null -1
//
struct TreeNode {
Tree Left;
Tree Right;
}T[MaxTree];
//
typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode {
ElementType *Data;
Position Front;
Position Rear;
int MaxSize;
};
typedef PtrToQNode Queue;
Tree BuildTree(struct TreeNode T[], int N); //
void LeaveSearch(Tree R, int N);//
Queue CreateQueue(int MaxSize);//
int IsEmpty(Queue Q);//
void AddQ(Queue Q, ElementType X);//
ElementType DeleteQ(Queue Q);//
//
int main()
{
Tree R;//
int N;
scanf("%d\n",&N);
R = BuildTree(T, N); //
LeaveSearch(R, N);//
return 0;
}
Tree BuildTree(struct TreeNode T[], int N)
{
int i; //??‘????±???°
Tree Root;
char cl, cr;
int check[10] = {0,};
if (!N) Root = Null;
if (N){ //??‘????o
for (i=0; i<N; i++) check[i] = 0;
for (i=0; i<N; i++){
scanf("%c %c\n",&cl, &cr);
if (cl != '-'){
T[i].Left = cl-'0';
check[T[i].Left ] = 1;
}else T[i].Left = Null;
if (cr != '-'){
T[i].Right = cr-'0';
check[T[i].Right ] = 1;
}else T[i].Right = Null;
}
for (i=0; i<N; i++) if (!check[i]) break;
Root = i;
}
return Root; //
}
//
void LeaveSearch(Tree R, int N)//
/**/
{
/*******************************
********************************/
Queue Q;
Tree R_1;
int flag = 1;
int cnt = 0;
Q = CreateQueue(MaxTree);
if (R == Null) printf("0");
if (T[R].Left == Null && T[R].Right == Null)
printf("%d",R);
AddQ(Q, R);//?°???1è????1R??’?…¥é????—
while (!IsEmpty(Q)){
R_1 = DeleteQ(Q);
if ((++cnt) == N) flag = 0;
if (T[R_1].Left == Null && T[R_1].Right == Null){
printf("%d",R_1);
if (flag) printf(" ");
}
if (T[R_1].Left != Null) AddQ(Q, T[R_1].Left );
if (T[R_1].Right != Null) AddQ(Q, T[R_1].Right );
}
}
Queue CreateQueue(int MaxSize)
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (ElementType*)malloc(MaxSize * sizeof(ElementType));
Q->Front = Q->Rear = 0;
Q->MaxSize = MaxSize;
return Q;
}
int IsEmpty(Queue Q)
{
if (Q->Front == Q->Rear) return 1;
else return 0;
}
void AddQ(Queue Q, ElementType X)
{
Q->Rear = (Q->Rear+1) % Q->MaxSize ;
Q->Data[Q->Rear ] = X;
}
ElementType DeleteQ(Queue Q)
{
Q->Front = (Q->Front+1)% Q->MaxSize ;
return Q->Data[Q->Front ];
}
是否同一棵二叉搜索树
给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树。
输入
输入包含若干组测试数据。每组数据的第1行给出两个正整数N (≤10)和L,分别是每个序列插入元素的个数和需要检查的序列个数。第2行给出N个以空格分隔的正整数,作为初始插入序列。最后L行,每行给出N个插入的元素,属于L个需要检查的序列。
简单起见,我们保证每个插入序列都是1到N的一个排列。当读到N为0时,标志输入结束,这组数据不要处理。
输出
对每一组需要检查的序列,如果其生成的二叉搜索树跟对应的初始序列生成的一样,输出“Yes”,否则输出“No”。
样例输入
4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0
样例输出
Yes
No
No
#include<stdio.h>
#include<stdlib.h>
typedef struct binaryTree* BST;
struct binaryTree{
int data;
BST left;
BST right;
};
BST Insert(BST T,int X)
{
if(T==NULL)
{
T=(struct binaryTree*)malloc(sizeof(struct binaryTree));
T->data=X;
T->left=T->right=NULL;
}
else{
if(T->data>X)
{
T->left=Insert(T->left,X);
}
else if(T->data<X)
{
T->right=Insert(T->right,X);
}
}
return T;
}
int cmp(BST T1,BST T2)
{
if(T1==NULL&&T2==NULL)
return 1;
if(T1&&T2)
if(T1->data==T2->data)
if(cmp(T1->left,T2->left)&&cmp(T1->right,T2->right))
return 1;
return 0;
}
int main(){
BST T;T=NULL;BST T2;
int N,L;int i;
while(scanf("%d",&N)!=EOF)
{
if(N==0)return 0;
scanf("%d",&L);
T=NULL;
int temp;
for(i=0;i<N;i++)
{
scanf("%d",&temp);
T=Insert(T,temp);
}
while(L--)
{
int temp2;T2=NULL;
for(i=0;i<N;i++)
{
scanf("%d",&temp2);
T2=Insert(T2,temp2);
}
if(cmp(T,T2))printf("Yes\n");
else printf("No\n");
}
}
}
Root of AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.
输入
Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.
输出
For each test case, print the root of the resulting AVL tree in one line.
样例输入
5
88 70 61 96 120
样例输出
70
#include <stdio.h>
#include <stdlib.h>
typedef struct AVLNode *AVLTree;
typedef struct AVLNode *Position;
struct AVLNode {
int Data;
int Height;
AVLTree Left;
AVLTree Right;
};
AVLTree Insert(int X, AVLTree T);
int GetHeight(Position T);
int Max(int a, int b);
Position SingleLeft(Position K);
Position SingleRight(Position K);
Position DoubleLeft(Position K);
Position DoubleRight(Position K);
int main(void) {
AVLTree T = NULL;
int n;
scanf("%d", &n);
while (n--) {
int x;
scanf("%d", &x);
T=Insert(x, T);
}
if (T)
printf("%d", T->Data);
return 0;
}
AVLTree Insert(int X, AVLTree T) {
if (T == NULL) {
T = (AVLTree)malloc(sizeof(struct AVLNode));
T->Data = X;
T->Height = 0;
T->Left = T->Right = NULL;
}
else if (X < T->Data) {
T->Left = Insert(X, T->Left);
if (GetHeight(T->Left) - GetHeight(T->Right) == 2) {
if (X < T->Left->Data)
T = SingleLeft(T);
else
T = DoubleLeft(T);
}
}
else if (X > T->Data) {
T->Right = Insert(X, T->Right);
if (GetHeight(T->Right) - GetHeight(T->Left) == 2) {
if (X > T->Right->Data)
T = SingleRight(T);
else
T = DoubleRight(T);
}
}
T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + 1;
return T;
}
int GetHeight(Position T) {
if (!T)
return -1;
else
return T->Height;
}
int Max(int a, int b) {
return (a > b) ? a : b;
}
Position SingleLeft(Position K) {
Position Tmp;
Tmp = K;
K = K->Left;
Tmp->Left = K->Right;
K->Right = Tmp;
Tmp->Height = Max(GetHeight(Tmp->Left), GetHeight(Tmp->Right)) + 1;
K->Height = Max(GetHeight(K->Left), GetHeight(K->Right)) + 1;
return K;
}
Position SingleRight(Position K) {
Position Tmp;
Tmp = K;
K = K->Right;
Tmp->Right = K->Left;
K->Left = Tmp;
Tmp->Height = Max(GetHeight(Tmp->Left), GetHeight(Tmp->Right)) + 1;
K->Height = Max(GetHeight(K->Left), GetHeight(K->Right)) + 1;
return K;
}
Position DoubleLeft(Position K) {
K->Left = SingleRight(K->Left);
return SingleLeft(K);
}
Position DoubleRight(Position K) {
K->Right = SingleLeft(K->Right);
return SingleRight(K);
}
第八章 查找
电话聊天狂人
试题描述
给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人。
输入
输入首先给出正整数N(≤105 ),为通话记录条数。随后N行,每行给出一条通话记录。简单起见,这里只列出拨出方和接收方的11位数字构成的手机号码,其中以空格分隔。
输出
在一行中给出聊天狂人的手机号码及其通话次数,其间以空格分隔。如果这样的人不唯一,则输出狂人中最小的号码及其通话次数,并且附加给出并列狂人的人数。
样例输入
4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832
样例输出
13588625832 3
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAXD 1000000
#define MAXNUMBER 12
typedef struct list
{
int count;
char telenumber[MAXNUMBER];
struct list *Next;
} *LNode;
typedef struct Map
{
int size;
LNode Head;
} *HashMap;
int NewPrime(int numbers)
{
int i,j,temp;
for(i=numbers+1;i<MAXD;i+=2)
{
temp = sqrt(i);
for(j=2;j<=temp;j++)
if(i%j == 0)
break;
if(j == temp + 1)
return i;
}
}
int Hash(char *key,int p)
{
return (atoi(key+MAXNUMBER-5) % p);
}
LNode Find(HashMap H,char* str)
{
int pos = Hash(str,H->size);
LNode position = H->Head[pos].Next;
while(position)
{
if(!strcmp(str,position->telenumber))
break;
else
position = position->Next;
}
return position;
}
void Insert(char* str,HashMap H)
{
int pos;
LNode position;
position = Find(H,str);
if(!position)
{
position = (LNode)malloc(sizeof(struct list));
pos = Hash(str,H->size);
strcpy(position->telenumber,str);
if(position->telenumber)
{
position->count = 1;
position->Next = H->Head[pos].Next;
H->Head[pos].Next = position;
}
}
else
position->count++;
return;
}
HashMap CreateHashMap(int numbers)
{
int i;
HashMap H;
char str[MAXNUMBER];
H = (HashMap)malloc(sizeof(struct Map));
H->size = NewPrime(2*numbers);
H->Head = (LNode)malloc(H->size *sizeof(struct list));
for(i=0;i<H->size;i++)
{
H->Head[i].Next = NULL;
H->Head[i].count = 0;
H->Head[i].telenumber[0] ='\0';
}
for(i=0;i<2*numbers;i++)
{
scanf("%s",&str);
Insert(str,H);
}
return H;
}
void Print(HashMap H)
{
LNode position;
int i,maxcount = 0,friends = 0;
char minnumber[MAXNUMBER];
for(i=0;i < H->size;i++)
{
position = H->Head[i].Next;
while(position)
{
if(position->count > maxcount)
{
maxcount = position->count;
strcpy(minnumber,position->telenumber);
friends = 1;
}
else if(position->count == maxcount)
{
friends++;
if(strcmp(position->telenumber,minnumber)<0)
strcpy(minnumber,position->telenumber);
}
position = position->Next;
}
}
printf("%s %d",minnumber,maxcount);
if(friends>1)
printf(" %d",friends);
return;
}
int main()
{
HashMap H;
int numbers,test;
char str[MAXNUMBER];
scanf("%d",&numbers);
H = CreateHashMap(numbers);
Print(H);
return 0;
}
hashing
试题描述
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.
Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.
输入
Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤104 ) and N (≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.
输出
For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print “-” instead.
样例输入
4 4
10 6 4 15
样例输出
0 1 4 -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10000
typedef struct HTable *HashTable;
struct HTable {
int *list;
int size;
};
HashTable CreatTable(int n);
int Insert(HashTable H, int num);
int NextPrime(int n);
int main() {
int i, m, n, num;
int pos[N];
HashTable H;
scanf("%d%d", &m, &n);
H = CreatTable(m);
for (i = 0; i < n; i++) {
scanf("%d", &num);
pos[i] = Insert(H, num);
}
if (n) {
printf("%d", pos[0]);
for (i = 1; i < n; i++) {
if (pos[i] == -1) printf(" -");
else printf(" %d", pos[i]);
}
}
return 0;
}
HashTable CreatTable(int n) {
HashTable H;
int i;
H = (HashTable)malloc(sizeof(struct HTable));
H->size = NextPrime(n);
H->list = (int*)malloc(H->size*sizeof(int));
for (i = 0; i < H->size; i++)
H->list[i] = 0;
return H;
}
int Insert(HashTable H, int num) {
int pos, i;
pos = num % H->size;
for (i = 0; i <= H->size/2 && H->list[pos]; i++) //?ï¿ ?é?? <= size/2
pos = (pos + 2 * i + 1) % H->size;
if (i <= H->size/2) {
H->list[pos] = 1;
return pos;
}
else return -1;
}
int NextPrime(int n) {
int i, j;
if (n == 1 || n % 2 == 0) //1?????ˉ?′???°
n++;
for (i = n;; i += 2) {
for (j = 3; j*j <= i && i%j; j++);
if (j*j > i) break;
}
return i;
}