当前位置:网站首页>团体程序设计天梯赛-练习集 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;
}
边栏推荐
- [go basics] 2 - go basic sentences
- Tri des fonctions de traitement de texte dans MySQL, recherche rapide préférée
- The right way to capture assertion failures in NUnit - C #
- string. Format without decimal places will generate unexpected rounding - C #
- 【性能测试】一文读懂Jmeter
- JVM -- class loading process and runtime data area
- 21个战略性目标实例,推动你的公司快速发展
- Chrome is set to pure black
- Zephyr learning notes 1, threads
- Const string inside function - C #
猜你喜欢

深入浅出:了解时序数据库 InfluxDB

The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)

zabbix监控系统邮件报警配置

论文学习——基于极值点特征的时间序列相似性查询方法

线性代数1.1

节点基础~节点操作
![[real case] how to deal with the failure of message consumption?](/img/ec/2bb2f0ff53c586bf45f364210658d7.jpg)
[real case] how to deal with the failure of message consumption?

L1-027 rental (20 points)

1. Qt入门

Go h*ck yourself:online reconnaissance (online reconnaissance)
随机推荐
How to set multiple selecteditems on a list box- c#
Wechat has new functions, and the test is started again
NPM run build error
L1-028 judging prime number (10 points)
Activiti常见操作数据表关系
Leetcode (215) -- the kth largest element in the array
L1-022 odd even split (10 points)
[go basics] 2 - go basic sentences
Zephyr Learning note 2, Scheduling
线性代数1.1
[test de performance] lire jmeter
Basic DOS commands
ZABBIX 5.0 monitoring client
Activiti常見操作數據錶關系
Practice (9-12 Lectures)
论文学习——基于极值点特征的时间序列相似性查询方法
MYCAT middleware installation and use
Easy to understand: understand the time series database incluxdb
Advanced MySQL: Basics (5-8 Lectures)
L1-030 one gang one (15 points)