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);
string
的insert()
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_queue
:priority_queue<Node, vector<Node>, greater<Node>> que;