#include <iostream>
#include <map>
#include <string>
#include <cstring>
using namespace std;
map<string, int> StringToint;
char p[10][11];
int spaces[10];
//定义空格数量
bool x_child_y(int x, int y)
{
// x是不是y的 孩子
if (spaces[x] - spaces[y] != 2)
{
return false;
}
for (int i = y + 1; i < x; i++)
{
if (spaces[i] < spaces[x])
{
return false;
}
}
return true;
//返回正确
}
bool x_parent_y(int x, int y)
{
return x_child_y(y, x);
}
// 判断是不是
bool x_sibiling_Y(int x, int y)
{
// 判断是否相同
if (spaces[x] != spaces[y])
{
return false;
}
if (x > y)
{
swap(x, y);
}
for (int i = x + 1; i < y; i++)
{
if (spaces[i] < spaces[y])
{
return false;
}
}
return true;
}
bool x_descendant_y(int x, int y)
{
//返回
if (spaces[x] < spaces[y])
{
return false;
}
for (int i = y + 1; i < x; i++)
{
if (spaces[i] == spaces[y])
{
return false;
}
}
return true;
}
bool x_ancestor_y(int x, int y)
{
// 返回值
return x_descendant_y(y, x);
}
void result(int x, int y, char *a)
{
if (strcmp(a, "child") == 0)
{
// 这个类型是 const * 类型的
if (x_child_y(x, y))
{
cout << true;
}
cout << false;
}
if (strcmp(a, "parent") == 0)
{
if (x_parent_y(x, y))
{
cout << true;
}
else
cout << false;
}
if (strcmp(a, "descendant"))
{
if (x_descendant_y(x, y))
{
cout << true;
}
else
cout << false;
}
if (strcmp(a, "sibiling") == 0)
{
if (x_sibiling_Y(x, y))
{
cout << true;
}
else
cout << false;
}
if (strcmp(a, "ancestor") == 0)
{
if (x_ancestor_y(x, y))
{
cout << true;
}
else
cout << false;
}
}
int main()
{
// 使用map 会快一点 但是 , 在比较空格数量的时候需要别的数组 存储结果
char c; //定义变量
char name[11];
int space = 0;
int number, edge;
cin >> number >> edge;
//输入 人数,以及判断
for (int i = 0; i < number; i++)
{
space = 0;
while ((c = getchar()) == ' ')
{
space++;
}
spaces[i] = space;
space = 1;
name[0] = c;
while ((c = getchar()) != '\n')
{
name[space++] = c;
}
name[space] = '\0';
StringToint[name] = spaces[i];
//结束标志
strcpy(p[i], name);
}
for (int i = 0; i < edge; i++)
{
//输入陈述句
char str1[11], str2[11], str3[11], str4[11], str5[11], str6[11];
scanf("%s %s %s %s %s %s", str1, str2, str3, str4, str5, str6);
int re1, re2;
re1 = StringToint[str1];
re2 = StringToint[str2];
result(re1,re2,str4);
//顺序不能错
}
}
参考文档:https://blog.csdn.net/qq_41231926/article/details/84841386