曾经我建堆还是挺顺的…好久没写数据结构的题,今天一写,疯狂出bug,我emo了…
题目
输入样例:
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.建立小根堆,略
2.atoi(char * str):
int atoi(const char *str );
将字符串(c语言的) 取出数字部分并转化成int类型的值并返回。
(第一个字符非数字字符直接返回0,如果是数字字符取到第一个不是数字字符为止)
如果该输入无法转换为该类型的值,则atoi的返回值为 0。
int main()
{
string str1 = "abc123d";
cout << atoi(str1.c_str()) << endl;//0
string str2 = "123d";
cout << atoi(str2.c_str()) << endl;//123
string str3 = "12.34";
cout << atoi(str3.c_str()) << endl;//12
string str4 = "abcd";
cout << atoi(str4.c_str()) << endl;//0
string str5 = "-123d";
cout << atoi(str5.c_str()) << endl;//-123
string str6 = "+123d";
cout << atoi(str6.c_str()) << endl;//123
string str7 = " 1234";
cout << atoi(str7.c_str());//1234,会自动忽略掉数字字符前面的空格
return 0;
}
3.string.c_str():
const char *c_str();
.c_str()函数返回一个指向正规C字符串(char*)的指针常量, 内容与string串相同。
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
4.string.substr(begin_pos,length):截取string的子串,从begin_pos开始,截取length个字符。
begin_pos:
如果是0或正整数,则代表字符串截取的起始下标
如果是负数,则代表从倒数第几个字符开始截取
length:
截取字符长度,不写默认截取到串尾。
int main()
{
string str = "abcdef";
cout << str.substr(0, 2) << endl;//ab
cout << str.substr(1, 2) << endl;//bc
cout << str.substr(1) << endl;//bcdef
return 0;
}
5.string.find(key_value,begin_pos):
注意前面是子串,后面是起始位置,我老是写反,然后就是疯狂debug。
从下标begin_pos开始寻找子串key_value,begin_pos没写默认从下标为0的位置开始找。
返回值是子串在母串中第一次出现的位置,如果没有找到,那么会返回一个特别的标记npos。(返回值可以看成是一个int型的数)
用在判断if (str.find(“abc”) != str.npos)
int main()
{
string str = "abcdefgabcdef";
cout << str.find("de") << endl;//3
cout << str.find("de", 3) << endl;//3
cout << str.find("de", 4) << endl;//10
cout << str.find("hello world") << endl;//18446744073709551615(npos)
return 0;
}
接着就是题目的完整代码了。
#include<iostream>
#include<map>
#include<string>
#include<cmath>
using namespace std;
int tree[1010];
map<int,int> father;
int main()
{
int n, m;
cin >> n >> m;
int len = 0;
//建树
tree[0] = -0x3f3f3f3f;
//将tree[0]设为无穷小,任何节点都不能和他交换位置
for (int i = 0; i < n; i++) {
int k;
len++;
cin >> k;
tree[len] = k;
for (int j = len; tree[j]<tree[j/2]; j /= 2) {
swap(tree[j], tree[j/2]);
//如果新的节点数据比它的父节点的数据小,交换。
}
}
//标记所以节点的父节点
father[tree[1]] = tree[0];
for (int i = 2; i <= n; i++)
father[tree[i]] = tree[i / 2];
getchar();
for (int i = 0; i < m; i++) {
int x, y;
string str;
getline(cin, str);
if (str.find("root")!=-1) {
string temp = str.substr(0, str.find(' '));
x = atoi(temp.c_str());
if (father[x] == tree[0]) cout << "T" << endl;
else cout << "F" << endl;
}
else if (str.find("siblings")!=-1) {
string str1 = str.substr(0, str.find(" "));
x = atof(str1.c_str());
string str2 = str.substr(str.find("and") + 4);
//会自动忽略掉数字字符前面的空格
y = atof(str2.c_str());
if (father[x] == father[y])
cout << "T" << endl;
else cout << "F" << endl;
}
else if (str.find("parent")!=-1) {
string str1 = str.substr(0, str.find(" "));
x = atof(str1.c_str());
string str2 = str.substr(str.find("of") + 3);
y = atof(str2.c_str());
if (father[y] == x)
cout << "T" << endl;
else cout << "F" << endl;
}
else {
string str1 = str.substr(0, str.find(" "));
x = atof(str1.c_str());
string str2 = str.substr(str.find("of") + 3);
y = atof(str2.c_str());
if (father[x] == y)
cout << "T" << endl;
else cout << "F" << endl;
}
}
return 0;
}
结束,真的是,有些东西一整子不用就忘得差不多了…
还好有训练赛,不然直接天梯得把队友坑死…