当前位置:网站首页>团体程序设计天梯赛-练习集 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;
}
边栏推荐
- Set and modify the page address bar icon favicon ico
- zabbix監控系統自定義監控內容
- Go h*ck yourself:online reconnaissance (online reconnaissance)
- Advanced MySQL: Basics (5-8 Lectures)
- L1-023 output gplt (20 points)
- 【性能測試】一文讀懂Jmeter
- Docker install MySQL
- Leetcode (215) -- the kth largest element in the array
- 墨者学院-PHPMailer远程命令执行漏洞溯源
- University stage summary
猜你喜欢
Zephyr 學習筆記2,Scheduling
【性能测试】一文读懂Jmeter
System architecture design of circle of friends
深入浅出:了解时序数据库 InfluxDB
Unity-Text上标平方表示形式+text判断文本是否为空
This article is enough for learning advanced mysql
弈柯莱生物冲刺科创板:年营收3.3亿 弘晖基金与淡马锡是股东
SQL注入测试工具之Sqli-labs下载安装重置数据库报错解决办法之一(#0{main}thrown in D:\Software\phpstudy_pro\WWW\sqli-labs-……)
墨者学院-PHPMailer远程命令执行漏洞溯源
大学阶段总结
随机推荐
Zephyr 学习笔记1,threads
With excellent strength, wangchain technology, together with IBM and Huawei, has entered the annual contribution list of "super ledger"!
Activiti常見操作數據錶關系
[go basics] 2 - go basic sentences
【Go基础】2 - Go基本语句
猜数字游戏
Text processing function sorting in mysql, quick search of collection
Leetcode(215)——数组中的第K个最大元素
L1-027 rental (20 points)
Moher College webmin unauthenticated remote code execution
Sqli labs download, installation and reset of SQL injection test tool one of the solutions to the database error (# 0{main}throw in d:\software\phpstudy_pro\www\sqli labs-...)
R language uses cforest function in Party package to build random forest based on conditional inference trees, uses varimp function to check feature importance, and uses table function to calculate co
Zephyr learning notes 1, threads
How to use MOS tube to realize the anti reverse connection circuit of power supply
Jianmu continuous integration platform v2.2.2 release
What determines vacuum permittivity and vacuum permeability? Why do these two physical quantities exist?
ZABBIX monitoring system custom monitoring content
Leetcode 146. LRU cache
如何用MOS管来实现电源防反接电路
墨者学院-Webmin未经身份验证的远程代码执行