7-13 关于堆的判断 (25分)
将一系列给定数字顺序插入一个初始为空的小顶堆H[]。随后判断一系列相关命题是否为真。命题分下列几种:
- x is the root:x是根结点;
- x and y are siblings:x和y是兄弟结点;
- x is the parent of y:x是y的父结点;
- x is a child of y:x是y的一个子结点。
输入格式:
每组测试第1行包含2个正整数N(≤ 1000)和M(≤ 20),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[−10000,10000]内的N个要被插入一个初始为空的小顶堆的整数。之后M行,每行给出一个命题。题目保证命题中的结点键值都是存在的。
输出格式:
对输入的每个命题,如果其为真,则在一行中输出T,否则输出F。
输入样例:
5 4
46 23 26 24 10
24 is the root
26 and 23 are siblings
46 is the parent of 23
23 is a child of 10
输出样例:
F
T
F
T
首先吸取堆的操作那道题的教训,它属于添加了全部元素后再调整的类型。然后就是命题的读入和判断这两个部分了。
- 命题的读入的策略可以有两种:
1.将命题拆分成一个个的单词,if-else分支来按单词读入。
2.用C语言中的getline函数或getchar逐个读入。(选这个!上面那个有点烦) - 命题的判断,做字符串匹配,因为这儿用了C风格的字符串,可以用strstr函数来匹配子串判断是哪一种命题。具体判断时要注意可能有负数出现作为最小堆的元素。然后具体的判断就是对堆这颗完全二叉树的性质的应用了。
- 0号结点应该是根节点。
- 兄弟结点的双亲相同
- 设双亲结点的下标值p_uline,孩子结点的下标值为c_uline,那么(c_uline - 1) / 2 == p_uline
需要注意的是这个判断条件必须是充分的,比如判断兄弟结点siblings。兄弟结点的下标值一定相差1,但结点的下标值相差1的结点不一定是兄弟结点,可以减一除二判等来看双亲是否相同。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
int InputStr(char *arr);//读入一个C风格的字符序列,'\n'作为终止读入的标志
//返回读入字符串的长度
int find_number(char* str, int i);//从字符数组下标为i的元素开始找一串数字
//序列并返回这个数,可以是以'+'或者'-'开头的任意长度的数字序列
void judge_expreesion(char* view, vector<int> first_heap);
//判断命题(每个字符序列)对于最小堆描述的真假
int main()
{
int nodes_num, expressions_num;
scanf("%d%d", &nodes_num, &expressions_num);
vector<int> first_heap;//用标准库建的最小堆存储向量
make_heap(first_heap.begin(), first_heap.end(), greater<int>());
int i, insert_number;
for (i = 0; i < nodes_num; i++)
{
scanf("%d", &insert_number);
first_heap.push_back(insert_number);
push_heap(first_heap.begin(), first_heap.end(), greater<int>());
}
//将一系列给定数字顺序插入一个初始为空的最小堆
getchar();//吃掉scanf留下的'\n'字符
char expressions[20][48];//用来存储命题的字符数组
//逐个判断所给命题
for(int i = 0; i < expressions_num; i++)
{
InputStr(expressions[i]);
judge_expreesion(expressions[i], first_heap);
}
return 0;
}
int InputStr(char *arr)
{
char alpha;
int i = 0;
while((alpha = getchar()) != '\n')
{
arr[i] = alpha;
i++;
}
return i;
}
int find_number(char* str, int i)
{
int number = 0, ten = 1, number_length = 0, j;
//找到数字中第一个元素的下标
while(!(str[i] >= '0' && str[i] <= '9') && (str[i] != '-' && str[i] != '+'))
i++;
if(str[i] != '-' || str[i] == '+')//带'+'或'-'的数字
{
int str_len = strlen(str);
for(j = i; j < str_len; j++)//获得数字的长度
{
if(str[j] >= '0' && str[j] <= '9')
number_length++;
else
break;
}
for(j = i + number_length - 1; j >= i; j--)//计算这个数字序列
//对应数字的值
{
number += ten * (int)(str[j] - '0');
ten *= 10;
}
}
else//不带'+'或'-'的数字
{
i++;
int str_len = strlen(str);
for(j = i; j < str_len; j++)
{
if(str[j] >= '0' && str[j] <= '9')//获得数字的长度
number_length++;
else
break;
}
for(j = i + number_length - 1; j >= i; j--)//计算这个数字序列
//对应数字的值
{
number += ten * (int)(str[j] - '0');
ten *= 10;
}
number *= -1;
}
return number;
}
void judge_expreesion(char* view, vector<int> first_heap)
{
if(strstr(view, "root") != NULL)//与root根相关的命题
{
int number = find_number(view, 0);
if(number == first_heap[0])
printf("T\n");
else
printf("F\n");
return;
}
if(strstr(view, "siblings") != NULL)//与siblings兄弟相关的命题
{
int lbrother, rbrother;
lbrother = find_number(view, 0);
rbrother = find_number(view, 6);
vector<int>::iterator l_uline = first_heap.begin(), r_uline = first_heap.begin();
l_uline = find(first_heap.begin(), first_heap.end(), lbrother);
r_uline = find(first_heap.begin(), first_heap.end(), rbrother);
if(abs(l_uline - r_uline) == 1 &&
((int)(l_uline - first_heap.begin()) - 1) / 2
== ((int)(r_uline - first_heap.begin()) - 1) / 2)
printf("T\n");
else
printf("F\n");
return;
}
if(strstr(view, "parent") != NULL)//与parent双亲特性相关的命题
{
int parent_value, child_value;
parent_value = find_number(view, 0);
child_value = find_number(view, 19);
vector<int>::iterator p_uline = first_heap.begin(), c_uline = first_heap.begin();
p_uline = find(first_heap.begin(), first_heap.end(), parent_value);
c_uline = find(first_heap.begin(), first_heap.end(), child_value);
if((((int)(c_uline - first_heap.begin()) - 1) / 2) == (int)(p_uline - first_heap.begin()))
printf("T\n");
else
printf("F\n");
return;
}
if(strstr(view, "child") != NULL)//与child孩子特性相关的命题
{
int parent_value, child_value;
child_value = find_number(view, 0);
parent_value = find_number(view, 14);
vector<int>::iterator p_uline = first_heap.begin(), c_uline = first_heap.begin();
p_uline = find(first_heap.begin(), first_heap.end(), parent_value);
c_uline = find(first_heap.begin(), first_heap.end(), child_value);
if((((int)(c_uline - first_heap.begin()) - 1) / 2) == (int)(p_uline - first_heap.begin()))
printf("T\n");
else
printf("F\n");
return;
}
}