[算法题] 模拟笔试 - 卡码网周赛第二十三期

卡码网周赛第二十三期

135. 获取连通的相邻节点列表

题解

在这里插入图片描述
这道题目没有什么难度,先用一个unordered_set 存储节点 A 所有的 VLAN_ID,然后读取每一行的与节点 A 相邻的节点的 TB 和端口信息时,如果端口的 VLAN_ID 可以在unordered_set 查询到,则加入结果即可

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <unordered_set>
#include <set>

int main()
{
    int M = 0;
    std::cin >> M;
    std::unordered_set<int> a_vlan_set;
    for (int i = 0; i < M; ++i) {
        int a_vlan;
        std::cin >> a_vlan;
        a_vlan_set.insert(a_vlan);
    }
    
    int N;
    std::cin >> N;
    
    // 忽略前两行之后的换行符
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    
    std::set<long long> connected_tbs;
    for (int i = 0; i < N; ++i) {
        std::string line;
        std::getline(std::cin, line);
        std::stringstream ss(line);
        long long tb = 0ll;
        int m = 0;
        ss >> tb >> m;
        for (int j = 0; j < m; ++j) {
            int tb_vlan;
            ss >> tb_vlan;
            if (a_vlan_set.find(tb_vlan) != a_vlan_set.end()) {
                connected_tbs.insert(tb);
                break;
            }
        }
    }
    
    std::cout << connected_tbs.size() << std::endl;
    for (auto& connected_tb : connected_tbs) {
        std::cout << connected_tb << " ";
    }
    return 0;
}

注意

  • 在读取完前两行后再使用getline,需要清空缓冲区中的换行符
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

136. 字符串处理器

题解

在这里插入图片描述
一道比较正常的模拟题,按照题目要求模拟即可

#include <iostream>
#include <string>

class StringProcessor {
public:
    StringProcessor():s_(), cursor_(0) {}
    void insert(const std::string& str) {
        s_.insert(cursor_, str);
        cursor_ += str.size();
    }
    void delete_left(int len) {
        if (len < 0 || len > cursor_) {
            return;
        }
        s_.erase(cursor_ - len, len);
        cursor_ -= len;
    }
    void move(int cnt) {
        if (cursor_ + cnt < 0 || cursor_ + cnt > s_.size()) {
            return;
        }
        cursor_ += cnt;
    }
    void copy() {
        s_.insert(cursor_, s_.substr(0, cursor_));
    }
    friend std::ostream& operator<<(std::ostream& os, const StringProcessor& s);
private:
    std::string s_;
    int cursor_;
};

std::ostream& operator<<(std::ostream& os, const StringProcessor& sp) {
    os << sp.s_.substr(0, sp.cursor_) << "|" <<  sp.s_.substr(sp.cursor_, sp.s_.size() -  sp.cursor_);
    return os;
}

int main()
{
    StringProcessor sp;
    while (true) {
        std::string line;
        std::cin >> line;
        if (line == "insert") {
            std::string str;
            std::cin >> str;
            sp.insert(str);
        } else if (line == "delete") {
            int len;
            std::cin >> len;
            sp.delete_left(len);
        } else if (line == "move") {
            int cnt;
            std::cin >> cnt;
            sp.move(cnt);
        } else if (line == "copy") {
            sp.copy();
        } else if (line == "end") {
            break;
        }
    }
    std::cout << sp << std::endl;
    return 0;
}

注意

  • cout 友元的写法 friend std::ostream& operator<<(std::ostream& os, const StringProcessor& s);
  • stringinsert() erase() substr() 的用法

137. 消息传输

题解

在这里插入图片描述
这道题显然是要使用广度优先搜索,核心是要先搜先到的点,用一个优先队列来维护
在这里插入图片描述

#include<iostream>
#include<vector>
#include<queue>

using namespace std;

struct Node{
    int x, y;
    int reach_time;
    Node(int new_x, int new_y, int new_reach_time) {
        x = new_x;
        y = new_y;
        reach_time = new_reach_time;
    }
    bool operator>(const Node& other) const {
        return this->reach_time > other.reach_time;
    }
};

int main()
{
    int n, m;
    cin >> n >> m;
    int start_x, start_y;
    cin >> start_x >> start_y;
    vector<vector<int>> graph(m, vector<int>(n, 0));
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> graph[i][j];
        }
    }
    const int dir[4][2] = {{-1,0}, {0, -1}, {1, 0}, {0, 1}};
    vector<vector<bool>> visited(m, vector<bool>(n, false));
    priority_queue<Node, vector<Node>, greater<Node>> que; // 小根堆
    que.push(Node(start_x, start_y, graph[start_x][start_y]));
    visited[start_x][start_y] = true;
    int max_time = 0;
    while (!que.empty()) {
        Node cur = que.top();
        que.pop();
        for (int i = 0; i < 4; ++i) {
            int next_x = cur.x + dir[i][0];
            int next_y = cur.y + dir[i][1];
            if (next_x < 0 || next_x >= m || next_y < 0 || next_y >= n 
                || visited[next_x][next_y] || graph[next_x][next_y] == 0) {
                continue;
            } 
            max_time = max(max_time, cur.reach_time);
            que.push(Node(next_x, next_y, cur.reach_time + graph[next_x][next_y]));
            visited[next_x][next_y] = true;
        }
    }
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            if (visited[i][j] == false && graph[i][j] != 0) {
                cout << -1 << endl;
                return 0;
            }
        }
    }
    cout << max_time << endl;
    return 0;
}

注意

  • 如何自定义 priority_queuepriority_queue<Node, vector<Node>, greater<Node>> que;
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值