华为机考真题 -- 电脑病毒感染

题目描述:

一个局域网内有很多台电脑,分别标注为 0 ~ N-1 的数字,相连接的电脑距离不一样,所以感染时间不一样,感染时间用t 表示。

其中网络内一台电脑病毒感染,求其感染网络内所有的电脑最少需要多长时间。如果最后有电脑不会感染,则返回-1。

给定一个数组 times 表示一台电脑把相邻电脑感染所用的时间。

如:path[i] = {i, j, t} 表示:电脑 i->j,电脑 i 上的病毒感染 j,需要
时间 t。

输入描述:

4
3
2 1 1
2 3 1
3 4 1
2

输入数据特别说明:
第1行,输入局域网内电脑个数N,1<=N<=200;
第2行,输出总共多少条网络连接M;
第3行到第3+M-1行,输入电脑连接情况以及病毒传染时间,如2 1 1,表示
病毒从电脑2传染到电脑1的时间为1
最后一行,输出病毒最开始所在电脑编号Num;

输出描述:

2

C++源码:

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <climits>

using namespace std;

// 封装的核心函数
int findMaxDistance(int computerNums, int linkNums, int source) {
    unordered_map<int, vector<pair<int, int>>> network;
    for (int item = 0; item < linkNums; ++item) {
        int i, j, k;
        cin >> i >> j >> k;
        network[i].push_back({ j, k });
        // 如果需要无向图,可以在这里添加 network[j].push_back({i, k});
    }

    vector<int> target(computerNums + 1, INT_MAX);
    target[source] = 0;

    vector<int> compare{ source };
    vector<bool> vd(computerNums + 1, false);
    vd[source] = true;

    while (!compare.empty()) {
        int cur = compare.back();
        compare.pop_back();
        vd[cur] = false;

        auto it = network.find(cur);
        if (it != network.end()) {
            for (const auto& edge : it->second) {
                int i = edge.first, j = edge.second;
                int count = target[cur] + j;
                if (target[i] <= count) continue;

                target[i] = count;
                if (vd[i]) continue;

                vd[i] = true;
                compare.push_back(i);
            }
        }
    }
    return *max_element(target.begin() + 1, target.end());
}

int main() {
    int computerNums, linkNums;
    cin >> computerNums >> linkNums;

    int source;
    cin >> source;

    int result = findMaxDistance(computerNums, linkNums, source);
    if (result == INT_MAX) {
        cout << -1 << endl;
    }
    else {
        cout << result << endl;
    }

    system("pause");
    return 0;
}

### 华为OD机考数大雁真题及答案解析 #### 题目描述 给定一个字符串 `croakOfFrogs`,表示同时间点听到的大雁叫声。每只大雁发出的声音序列严格遵循 "quack" 的顺序。返回能够产生所给字符串的最少大雁数量。如果该字符串是有效的组合,则返回 `-1`。 条件如下: - 输入字符串长度范围:\( 1 \leq croakOfFrogs.length \leq 10^5 \) - 字符串中的字符仅限于 'q', 'u', 'a', 'c' 或者 'k' #### 解决方案 为了计算最小的大雁数量,可以维护五个计数器来跟踪当前正在发声的同阶段的大雁数目。每当遇到一个新的起始字母(即 'q'),增加相应计数器;当完成一次完整的 “quack” 声音循环时减少这些计数器。还需要确保任何时候后面的字母会超过前面的字母的数量,否则就一个合法的输入[^1]。 下面是具体的实现方法: ```cpp class Solution { public: int minNumberOfGeese(string croakOfGeese) { unordered_map<char, int> count{{'q', 0}, {'u', 0}, {'a', 0}, {'c', 0}, {'k', 0}}; int max_geese = 0; for (char ch : croakOfGeese) { ++count[ch]; // Check the order of characters to ensure validity. if (!(count['q'] >= count['u'] && count['u'] >= count['a'] && count['a'] >= count['c'] && count['c'] >= count['k'])) { return -1; } // Update maximum number of geese at any point in time. max_geese = std::max(max_geese, *std::max_element(count.begin(), count.end(), [](const auto& p1, const auto& p2) { return p1.second < p2.second; })); // When a full sequence is completed ('quack'), decrement all counters by one. if (ch == 'k') { for (auto& pair : count) { --pair.second; } } } // Ensure no incomplete sequences are left over. for (int val : count.values()) { if (val != 0) return -1; } return max_geese; } }; ``` 此代码通过遍历整个字符串并保持对每个声音部分的追踪来解决问题。它还验证了每次读取新字符后的合法性,并在检测到完整的一轮发音后重置计数器。最后检查是否有未完成的序列存在,如果有则返回错误码 `-1`,否则返回最大并发大雁数量作为结果[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值