当前位置:网站首页>团体程序设计天梯赛-练习集 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;
}
边栏推荐
- ZABBIX monitoring system custom monitoring content
- JVM -- class loading process and runtime data area
- Zephyr 學習筆記2,Scheduling
- Zephyr learning notes 1, threads
- 谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展
- 促进OKR落地的工作总结该如何写?
- Life planning (flag)
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
- The idea of implementing charts chart view in all swiftui versions (1.0-4.0) was born
- 【Go基础】2 - Go基本语句
猜你喜欢
zabbix監控系統自定義監控內容
ZABBIX monitoring system custom monitoring content
线性代数1.1
Distributed transaction management DTM: the little helper behind "buy buy buy"
User login function: simple but difficult
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?
I was pressed for the draft, so let's talk about how long links can be as efficient as short links in the development of mobile terminals
Preliminary study on temporal database incluxdb 2.2
随机推荐
C#实现一个万物皆可排序的队列
Zephyr Learning note 2, Scheduling
[C language] open the door of C
Go h*ck yourself:online reconnaissance (online reconnaissance)
Node foundation ~ node operation
人生规划(Flag)
How to reset IntelliSense in vs Code- How to reset intellisense in VS Code?
NPM run build error
【性能測試】一文讀懂Jmeter
It's healthy to drink medicinal wine like this. Are you drinking it right
Mysql database - function constraint multi table query transaction
L1-024 the day after tomorrow (5 points)
Activiti常見操作數據錶關系
PCIE知识点-010:PCIE 热插拔资料从哪获取
Zephyr study notes 2, scheduling
Zephyr 学习笔记1,threads
21个战略性目标实例,推动你的公司快速发展
Amd RX 7000 Series graphics card product line exposure: two generations of core and process mix and match
What are the work contents of operation and maintenance engineers? Can you list it in detail?
MySQL 数据库 - 函数 约束 多表查询 事务