当前位置:网站首页>团体程序设计天梯赛-练习集 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;
}
边栏推荐
- PCIe knowledge points -010: where to get PCIe hot plug data
- 【性能测试】一文读懂Jmeter
- Cannot click button when method is running - C #
- zabbix监控系统部署
- zabbix監控系統自定義監控內容
- It's healthy to drink medicinal wine like this. Are you drinking it right
- Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)
- Leetcode 146. LRU 缓存
- Oracle stored procedures and functions
- Difference between static method and non static method (advantages / disadvantages)
猜你喜欢

Linear algebra 1.1

Zephyr study notes 2, scheduling

It's healthy to drink medicinal wine like this. Are you drinking it right

Go h*ck yourself:online reconnaissance (online reconnaissance)

论文学习——基于极值点特征的时间序列相似性查询方法
![[test de performance] lire jmeter](/img/c9/25a0df681c7ecb4a0a737259c882b3.png)
[test de performance] lire jmeter
![[performance test] read JMeter](/img/c9/25a0df681c7ecb4a0a737259c882b3.png)
[performance test] read JMeter

How to send mail with Jianmu Ci

Zephyr 學習筆記2,Scheduling

MySQL中的文本处理函数整理,收藏速查
随机推荐
线性代数1.1
In the era of low code development, is it still needed?
How does dataframe calculate the average value of each row as another column
[Gurobi] 简单模型的建立
Activiti常见操作数据表关系
Application of isnull in database query
Take you to master the formatter of visual studio code
Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
JVM -- class loading process and runtime data area
Zephyr 学习笔记1,threads
Common components of flask
Practice (9-12 Lectures)
Wechat has new functions, and the test is started again
1. Qt入门
Jianmu continuous integration platform v2.2.2 release
How to set multiple selecteditems on a list box- c#
Devops Practice Guide - reading notes (long text alarm)
Moher college phpMyAdmin background file contains analysis traceability
Types of references in BibTex
墨者学院-phpMyAdmin后台文件包含分析溯源