当前位置:网站首页>团体程序设计天梯赛-练习集 L2-002 链表去重
团体程序设计天梯赛-练习集 L2-002 链表去重
2022-07-04 07:53:00 【Cod_ing】
特点:采用Hash表
#include<iostream>
#include<unordered_map>
#include<map>
#include<string>
#include<vector>
using namespace std;
struct ListNode {
string addr;
string next;
int data;
};
int f_abs(int x) {
if (x > 0) return x;
return -x;
}
int main() {
unordered_map<string, ListNode> LinkList; //Hash表,存放原始链表
vector<ListNode> Deleted; //存放删除的链表节点
map<int, int> nums; //标记这个数是否存在
string head;
int n;
cin >> head >> n;
string addr, next;
int data;
int cur = 0;
for (int i = 0; i < n; i++) {
cin >> addr >> data >> next;
LinkList[addr].addr = addr;
LinkList[addr].data = data;
LinkList[addr].next = next;
}
LinkList["-1"]; //address=-1作为迭代的终止条件
string pre_node = head;
string node = LinkList[head].next;
nums[f_abs(LinkList[pre_node].data)] = 1; //先存放头节点的数
while (node!= "-1") {
int x = f_abs(LinkList[node].data);
nums[x]++;
if (nums[x]>1) {
//这个数之前出现过
if (cur > 0) {
//被删除节点链表的节点的前一个节点要特殊处理,即更改next值
Deleted[cur - 1].next = node;
}
Deleted.push_back({
node, "-1", LinkList[node].data }); //插入的节点next赋值为-1
cur++;
LinkList[pre_node].next = LinkList[node].next;
node = LinkList[node].next;
}
else {
pre_node = node;
node = LinkList[node].next;
}
}
//cout << endl;
node = head;
while (node != "-1") {
cout << node << " " << LinkList[node].data << " " << LinkList[node].next<<endl;
node = LinkList[node].next;
}
//cout << endl; 输出删除节点
for (ListNode x : Deleted) {
cout << x.addr << " " << x.data << " " << x.next << endl;
}
return 0;
}
边栏推荐
- L1-026 I love gplt (5 points)
- string. Format without decimal places will generate unexpected rounding - C #
- [Gurobi] 简单模型的建立
- MySQL中的文本處理函數整理,收藏速查
- zabbix监控系统自定义监控内容
- Système de surveillance zabbix contenu de surveillance personnalisé
- 1. Qt入门
- Parallel shift does not provide any acceleration - C #
- Advanced MySQL: Basics (5-8 Lectures)
- 【Go基础】1 - Go Go Go
猜你喜欢
运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)
Introduction to neural network (Part 2)
【性能測試】一文讀懂Jmeter
How to use MOS tube to realize the anti reverse connection circuit of power supply
[Gurobi] 简单模型的建立
It's healthy to drink medicinal wine like this. Are you drinking it right
时序数据库 InfluxDB 2.2 初探
Handwritten easy version flexible JS and source code analysis
Heap concept in JVM
谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展
随机推荐
Comparison between applet framework and platform compilation
How to set multiple selecteditems on a list box- c#
JVM -- class loading process and runtime data area
Is l1-029 too fat (5 points)
深入浅出:了解时序数据库 InfluxDB
墨者学院-phpMyAdmin后台文件包含分析溯源
[Chongqing Guangdong education] National Open University spring 2019 770 real estate appraisal reference questions
Project 1 household accounting software (goal + demand description + code explanation + basic fund and revenue and expenditure details record + realization of keyboard access)
ZABBIX monitoring system custom monitoring content
Set and modify the page address bar icon favicon ico
Oceanbase is the leader in the magic quadrant of China's database in 2021
论文学习——基于极值点特征的时间序列相似性查询方法
[real case] how to deal with the failure of message consumption?
人生规划(Flag)
zabbix监控系统邮件报警配置
运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)
Heap concept in JVM
How to write a summary of the work to promote the implementation of OKR?
Sort by item from the list within the list - C #
Distributed transaction management DTM: the little helper behind "buy buy buy"