L2-012 关于堆的判断 (25 分)(字符串bug待解决)

题目详情 - L2-012 关于堆的判断 (25 分) (pintia.cn)

已查出:字符串处理错误,还未能解决。

猜测:题目的测试点不标准,有尾随空格

#include<iostream>
#include<unordered_map>
using namespace std;
const int N = 1010, INF = 0x3f3f3f3f;
unordered_map<int,int> um;
int h[N],sz;

void insert(int x){
    h[++sz] = x;
    int cur = sz;
    while(cur/2 && h[cur/2] > h[cur]){
        swap(h[cur/2],h[cur]);
        cur /= 2;
    }
}

int main(){
    int n,m;
    cin>>n>>m;
    for(int i = 0; i < n; i++){
        int x;
        cin>>x;
        insert(x);
    }
    
    for(int i = 1; i <= n; i++) um[h[i]] = i;
    
    while(m--){
        string str;
        int x = INF, y = INF, op = INF;
        char c;
        while(cin>>str){
        	c = getchar();
//        	cout<<str<<endl;
            if( isdigit(str[0]) ) {
                if(x == INF) 
					x = stoi(str);
                else if(y == INF) 
					y = stoi(str);
            } else if(str == "root") 
				op = 1;
            else if(str == "siblings") 
				op = 2;
            else if(str == "parent") 
				op = 3;
            else if(str == "child") 
            	op = 4;
			
			if(c == '\n') break;
        }
        
        if(op == 1){
            if(um[x] == 1) puts("T");
            else puts("F");
        } else if(op == 2){
            if(um[x]/2 == um[y]/2) puts("T");
            else puts("F");
        } else if(op == 3){
            if(um[x] == um[y]/2) puts("T");
            else puts("F");    
        } else if(op == 4){
            if(um[x]/2 == um[y]) puts("T");
            else puts("F");    
        }
        
    }
    
    return 0;
}

AC代码

#include<iostream>
#include<unordered_map>
using namespace std;
const int N = 1010, INF = 0x3f3f3f3f;
unordered_map<int,int> mp;
int h[N],sz;

void insert(int x){
    h[++sz] = x;
    int cur = sz;
    while(cur/2 && h[cur/2] > h[cur]){
        swap(h[cur/2],h[cur]);
        cur /= 2;
    }
}

int main(){
    int n,m;
    cin>>n>>m;
    for(int i = 0; i < n; i++){
        int x;
        cin>>x;
        insert(x);
    }
    
    for(int i = 1; i <= n; i++) mp[h[i]] = i;
    
    for (int i = 0; i < m; ++i) {
        int a;
        scanf("%d", &a);
        string s1; cin >> s1;
        if (s1 == "and") {
            int b; scanf("%d", &b);
            cin >> s1 >> s1;
            if (mp[a] / 2 == mp[b] / 2)puts("T");
            else puts("F");
        }
        else {
            cin >> s1;
            if (s1 == "a") {
                cin >> s1 >> s1;
                int b; scanf("%d", &b);
                if (mp[a] / 2 == mp[b])puts("T");
                else puts("F");
            }
            else {
                cin >> s1;
                if (s1 == "root") {
                    if (mp[a] == 1)puts("T");
                    else puts("F");
                }
                else {
                    cin >> s1;
                    int b; scanf("%d", &b);
                    if (mp[b] / 2 == mp[a])puts("T");
                    else puts("F");
                }
            }
        }
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值