KY146 魔咒词典 && KY188 哈夫曼树

文章描述了一个编程挑战,要求创建一个程序帮助哈利波特记住和查找魔法世界的魔咒。程序需基于给定的魔咒词典,实现根据魔咒名字找功能,或根据功能找魔咒。此外,还有一道关于构建哈夫曼树的问题,目标是求解最小的权值乘积之和。
摘要由CSDN通过智能技术生成

描述
哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助。 给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”
输入描述:
首先列出词典中不超过100000条不同的魔咒词条,每条格式为: [魔咒] 对应功能 其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。词典最后一行以“@END@”结束,这一行不属于词典中的词条。 词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。
输出描述:
每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果魔咒不在词典中,就输出“what?”

输入:
[expelliarmus] the disarming charm
[rictusempra] send a jet of silver light to hit the enemy
[tarantallegra] control the movement of one’s legs
[serpensortia] shoot a snake out of the end of one’s wand
[lumos] light the wand
[obliviate] the memory charm
[expecto patronum] send a Patronus to the dementors
[accio] the summoning charm
@END@
4
[lumos]
the summoning charm
[arha]
take me to the sky

输出:
light the wand
accio
what?
what?

// 题目链接  https://www.nowcoder.com/practice/c6ca566fa3984fae916e6d7beae8ea7f?tpId=63&tqId=29593&tPage=2&ru=/kaoyan/retest/9001&qru=/ta/zju-kaoyan/question-ranking

#include<stdio.h>
#include<string>
#include<map>

using namespace std;

int main() {
    int n;
    map<string, string> dict;
    while (true) {
        char arr[100];
        fgets(arr, 100, stdin);
        string s = arr;
        s.pop_back();
        if (s == "@END@") {
            break;
        }
        // substr  左闭右开   一个参数表示 一直到字符串结尾
        string word = s.substr(0, s.find(']') + 1);
        string content = s.substr(s.find(']') + 2);
        dict[word] = content;
        dict[content] = word;
    }
    scanf("%d", &n);
    getchar();  //去除数字后的 \n
    for (int i = 0; i < n; ++i) {
        char a[100];
//        gets(a);  // gets() 后不会有 \n
        fgets(a, 100, stdin);
        string s1 = a;
        s1.pop_back();
        if (dict.find(s1) != dict.end()) {
            if (s1[0] == '[') {
                printf("%s\n", dict[s1].c_str());
            } else {
                printf("%s\n", (dict[s1].substr(1, dict[s1].size() - 2)).c_str());
            }
        } else {
            printf("what?\n");
        }
    }
    return 0;
}

描述
哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和的最小值。
输入描述:
输入有多组数据。 每组第一行输入一个数n,接着输入n个叶节点(叶节点权值不超过100,2<=n<=1000)。
输出描述:
输出权值。

输入:
5
1 2 2 5 9

输出:
37

// 题目链接  https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155?tpId=67&tqId=29635&tPage=1&ru=/kaoyan/retest/1005&qru=/ta/bupt-kaoyan/question-ranking

#include<stdio.h>
#include<queue>

using namespace std;


int main() {
    int n;
    int k;
    // 输出最大的数
    priority_queue<int> q;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &k);
        // 因为每次 都是取最小的两个值
        q.push(-k);
    }
    // 注: 后序用到 +res 时  res 必须付初值
    int res = 0;
    // 权值 等于 左子树的权值加 左节点的值 + 右子树的权值加 右节点的值
    while (q.size() > 1) {
        int l = q.top();
        q.pop();
        int r = q.top();
        q.pop();
        res = res + l + r;
        q.push(l + r);
    }
    printf("%d\n", -res);

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值