L2-012 关于堆的判断 (25 分) make_heap push_heap

将一系列给定数字顺序插入一个初始为空的小顶堆H[]。随后判断一系列相关命题是否为真。命题分下列几种:

  • x is the rootx是根结点;
  • x and y are siblingsxy是兄弟结点;
  • x is the parent of yxy的父结点;
  • x is a child of yxy的一个子结点。

输入格式:

每组测试第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代码

本题关键点就是一个个元素放进堆中,而不是将一整串元素转换为小顶堆 

比较繁杂的代码:

#include<bits/stdc++.h>
using namespace std;
int n,m;
vector<int> num;
regex reg("-?[0-9]+");   //判断时正负数都能接受
void root(string s)
{
    int arr[2]={0};
    int m=0,k=0;
    for(sregex_iterator it(s.begin(),s.end(),reg);it!=sregex_iterator();++it)
    {
        string str = it->str();
        arr[k++]=atoi(str.c_str());
    }
    if(arr[0]==num[1]) cout<<"T"<<endl;
    else cout<<"F"<<endl;
}

void siblings(string s)
{
    int arr[2]={0};
    int m=0,k=0;
    for(sregex_iterator it(s.begin(),s.end(),reg);it!=sregex_iterator();++it)
    {
        string str = it->str();
        arr[k++]=atoi(str.c_str());
    }
    auto a = find(num.begin(),num.end(),arr[0]);
    int b=distance(num.begin(),a);
    if(b%2==0) {
        if(num[b+1]==arr[1]) cout<<"T"<<endl;
        else cout<<"F"<<endl;
    }else
        if(num[b-1]==arr[1]) cout<<"T"<<endl;
        else cout<<"F"<<endl;
}

void parent(string s)
{
    int arr[2]={0};
    int m=0,k=0;
    for(sregex_iterator it(s.begin(),s.end(),reg);it!=sregex_iterator();++it)
    {
        string str = it->str();
        arr[k++]=atoi(str.c_str());
    }
    auto a = find(num.begin(),num.end(),arr[1]);
    int b=distance(num.begin(),a);
    if(num[b/2]==arr[0]) cout<<"T"<<endl;
    else cout<<"F"<<endl;
}

void child(string s)
{
    int arr[2]={0};
    int m=0,k=0;
    for(sregex_iterator it(s.begin(),s.end(),reg);it!=sregex_iterator();++it)
    {
        string str = it->str();
        arr[k++]=atoi(str.c_str());
    }
    auto a = find(num.begin(),num.end(),arr[0]);
    int b=distance(num.begin(),a);
    if(num[b/2]==arr[1]) cout<<"T"<<endl;
    else cout<<"F"<<endl;
}
int main()
{
    num.push_back(0);
    cin>>n>>m;
    int a;
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        num.push_back(a);
        make_heap(num.begin()+1,num.end(),greater<int>());
    }
    string s;
    getchar();
    for(int i=0;i<m;i++)
    {
        getline(cin,s);
        if(s.find("root")!=string::npos) root(s);
        else if(s.find("siblings")!=s.npos) siblings(s);
        else if(s.find("parent")!=s.npos) parent(s);
        else if(s.find("child")!=s.npos) child(s);
    }

    return 0;
}

 另一版本:


#include<bits/stdc++.h>
using namespace std;
int n,m,indexx=1;
int num[1005] , arr[2];
map<int , int > mmp;
regex reg("-?[0-9]+");   //判断时正负数都能接受
void creat(int data)
{
    if (indexx == 1)
    {
        num[1] = data;
        indexx++;
        return;
    }
    else
    {
        num[indexx] = data;
        int temp = indexx;
        while (temp >= 2 && num[temp] < num[temp / 2])
        {
            swap(num[temp / 2], num[temp]);
            temp /= 2;
        }
        indexx++;
    }
}
void finnum(string s)
{
    int k=0;
    for(sregex_iterator it(s.begin(),s.end(),reg);it!=sregex_iterator();++it)
    {
        string str = it->str();
        arr[k++]=atoi(str.c_str());
    }
}
void root(string s)
{
    finnum(s);
    if(mmp[arr[0]]==1) cout<<"T"<<endl;
    else cout<<"F"<<endl;
}

void siblings(string s)
{
    finnum(s);
    if( mmp[arr[0]]/2==mmp[arr[1]]/2 ) cout<<"T"<<endl;
    else   cout<<"F"<<endl;
}

void parent(string s)
{
    finnum(s);
    if( mmp[arr[0]]==mmp[arr[1]]/2 ) cout<<"T"<<endl;
    else   cout<<"F"<<endl;
}

void child(string s)
{
    finnum(s);
    if( mmp[arr[0]]/2==mmp[arr[1]] ) cout<<"T"<<endl;
    else   cout<<"F"<<endl;
}
int main()
{
    num[0]=-1;
    cin>>n>>m;
    int a;
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        creat(a);
    }
    for(int i = 1; i <= n; i ++) mmp[num[i]] = i;
    string s;
    getchar();
    for(int i=0;i<m;i++)
    {
        getline(cin,s);
        if(s.find("root")!=string::npos) root(s);
        else if(s.find("siblings")!=s.npos) siblings(s);
        else if(s.find("parent")!=s.npos) parent(s);
        else if(s.find("child")!=s.npos) child(s);
    }
    return 0;
}

使用make_heap / push_make 简化:

#include<bits/stdc++.h>
using namespace std;
int n,m;    
int num[1005] , arr[2];  //结点 , 查找的结点 //堆为完全二叉树,使用数组储存
map<int , int > mmp;     //堆结点为key 数组下标为value  由此记录小顶堆/完全二叉树的数组下标
regex reg("-?[0-9]+");   //判断时正负数都能接受

void finnum(string s)
{
    int k=0;
    for(sregex_iterator it(s.begin(),s.end(),reg);it!=sregex_iterator();++it)  //正则表达式匹配
    {
        arr[k++]=atoi(it->str().c_str()); //atoi是将数字字符数组转换为数字
        //it->str()返回的值为字符串,需要用.c_str()函数转为字符数组
    }
}

void root(string s)//根
{
    finnum(s);
    if(mmp[arr[0]]==1) cout<<"T"<<endl; //通过完全二叉树的下标对应性质来判断
    else cout<<"F"<<endl;
}

void siblings(string s)//兄弟
{
    finnum(s);
    if( mmp[arr[0]]/2==mmp[arr[1]]/2 ) cout<<"T"<<endl; //通过完全二叉树的下标对应性质来判断
    else   cout<<"F"<<endl;
}

void parent(string s)//父亲
{
    finnum(s);
    if( mmp[arr[0]]==mmp[arr[1]]/2 ) cout<<"T"<<endl; //通过完全二叉树的下标对应性质来判断
    else   cout<<"F"<<endl;
}

void child(string s)//孩子
{
    finnum(s);
    if( mmp[arr[0]]/2==mmp[arr[1]] ) cout<<"T"<<endl; //通过完全二叉树的下标对应性质来判断
    else   cout<<"F"<<endl;
}
int main()
{
    num[0]=-1;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>num[i];
        push_heap(num+1,num+1+i,greater<int>());
    }
    for(int i = 1; i <= n; i ++) mmp[num[i]] = i;
    string s;
    getchar();
    for(int i=0;i<m;i++)
    {
        getline(cin,s); //输入一整行 , 使用正则表达式取出数字 , 注意数字可能是正数或负数
        //使用string.find()函数来找单词,四个句子中都有一个其他句子都没有的单词,由此判断是哪个命题
        if(s.find("root")!=string::npos) root(s);
        else if(s.find("siblings")!=s.npos) siblings(s);
        else if(s.find("parent")!=s.npos) parent(s);
        else if(s.find("child")!=s.npos) child(s);
    }
    return 0;
}

另附上其他大佬的做法:

#include <iostream>
using namespace std;
int a[1005], index = 1;
int n, m;
void creat(int data)
{
    if (index == 1)
    {
        a[1] = data;
        index++;
        return;
    }
    else
    {
        a[index] = data;
        int temp = index;
        while (temp >= 2 && a[temp] < a[temp / 2])
        {
            swap(a[temp / 2], a[temp]);
            temp /= 2;
        }
        index++;
    }
}
int find1(int val)
{
    for (int i = 1; i <= n; i++)
    {
        if (a[i] == val)
            return i;
    }
    return -1;
}
int main()
{

    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        int temp;
        cin >> temp;
        creat(temp);
    }
    for (int i = 1; i <= m; i++)
    {
        int first, second;
        string str;
        cin >> first >> str;
        if (str == "and")
        {
            cin >> second >> str >> str;
            if (find1(first)/2 == find1(second) / 2)
            {
                cout << "T\n";
            }
            else
            {
                cout << "F\n";
            }
        }
        else
        {
            cin >> str;
            if (str[0] == 'a')
            {
                cin >> str >> str >> second;
                if (find1(first) / 2 == find1(second))
                {
                    cout << "T\n";
                }
                else
                {
                    cout << "F\n";
                }
            }
            else
            {
                cin >> str;
                if (str[0] == 'r')
                {
                    if (find1(first) == 1)
                    {
                        cout << "T\n";
                    }
                    else
                    {
                        cout << "F\n";
                    }
                }
                else
                {
                    cin >> str >> second;
                    if (find1(first) == find1(second) / 2)
                        cout << "T\n";
                    else
                        cout << "F\n";
                }
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值