您的位置:首页 > 其它

L2-012. 关于堆的判断

2017-04-12 01:22 288 查看
将一系列给定数字顺序插入一个初始为空的小顶堆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


建立小顶堆,每次输入数据都执行一次插入算法,顺便找个图记录每个叶节点的值和叶值最好。

代码如下:(后面有个代码是自己写完后网上找的AC代码,汗,自己写的总有几个样例过不去,请高手指点。)

//
//  main.cpp
//  L2-012. 关于堆的判断
//
//  Created by 徐智豪 on 2017/4/11.
//  Copyright © 2017年 徐智豪. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int level[30000]={0};
int N,M;
void adjust(int index)
{
while(level[index]<level[index/2])
{
int t=level[index];
level[index]=level[index/2];
level[index/2]=t;
index=index/2;
}
}
int main(int argc, const char * argv[]) {
scanf("%d%d",&N,&M);
int value,count=1;
level[0]=-30000;
for(int i=0;i<N;i++)
{
scanf("%d",&value);
level[count]=value;
adjust(count++);
// for(int i=1;i<count;i++)
//  cout<<level[i]<<" ";
//cout<<endl;
}
string s;
int x=0,y=0;
getchar();
for(int i=0;i<M;i++)
{
x=0,y=0;
getline(cin,s);
int flag=0;
int k=0;
int pc=0;
int t;
for(t=0;t<s.size();t++)
{
if(s[t]>='0'&&s[t]<='9'&&flag==0)
{
flag++;
while(s[t]!=' ')
{
x=x*10+s[t]-'0';
t++;
}
continue;
}
if(flag==1&&s[t]<='9'&&s[t]>='0')
{
flag++;
int temp=t;
while(s[t]>='0'&&s[t]<='9')
{
y=y*10+s[t]-'0';
t++;
}
if(s[temp-1]=='-')
y=y*-1;
continue;
}
if(s[t]=='p')
pc=3;
if(s[t]=='c')
pc=4;
}
if(s[0]=='-')x=x*-1;
if(flag==1&&s[s.size()-1]=='t')
{
if(level[1]==x)
cout<<"T"<<endl;
else cout<<"F"<<endl;
}
else if(flag==2)
{
if(s[s.size()-1]=='s')
{
k=1;
while(level[k]!=x)
k++;
if(level[k+1]==y||level[k-1]==y)
cout<<"T"<<endl;
else cout<<"F"<<endl;
}
if(pc==3)
{
k=1;
while(level[k]!=y)
k++;
if(level[k/2]==x)
cout<<"T"<<endl;
else cout<<"F"<<endl;
}
if(pc==4)
{
k=1;
while(level[k]!=x)
k++;
if(level[k/2]==y)
cout<<"T"<<endl;
else cout<<"F"<<endl;
}
}
}
return 0;
}




这个是网上AC的代码/

//
//  main.cpp
//  L2-012. 关于堆的判断
//
//  Created by 徐智豪 on 2017/4/11.
//  Copyright © 2017年 徐智豪. All rights reserved.
//

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>

using namespace std;

int n, a[1005];

//建树的 “模板”
void up( int son )
{
int t = a[son];
int tson = son;
while( (tson > 1)&&( a[tson/2] > t))
{
a[tson] = a[tson/2];
tson = tson/2;
}
a[tson] = t;
}

void charu( int  t)
{
a[ ++n ] = t;
up( n );
}

int main ()
{
int k, m, x, y;;
map <int, int> index; //记录下标
string s;
cin >> k >> m;
n=0;
//建树
for(int i=0; i<k; i++)
{
cin >> x;
charu(x);
}
//给map赋值
for(int i=1; i<=n; i++)
{
index[a[i]] = i;
}
for(int i=0; i<m; i++)
{
cin >> x;
cin >> s;
int index_x = index[x];
int index_y;
if(s[0] == 'a')
{
cin >> y;
getline(cin, s);  //这个函数可以输入一个带空格的字符串
index_y = index[y];
if(index_x/2 == index_y/2)
puts("T");
else
puts("F");
}
else
{
cin >> s;
cin >> s;
if(s[0] == 'r')
{
if(index_x == 1)
puts("T");
else
puts("F");
}
else if(s[0] == 'p')
{
cin >> s;
cin >> y;
index_y = index[y];
if(index_y/2 == index_x)
puts("T");
else
puts("F");

}
else
{
cin >> s;
cin >> y;
index_y = index[y];
if(index_x/2 == index_y)
puts("T");
else
puts("F");
}
}
}

return 0;
}


4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: