当前位置: 首页 > news >正文

L2-002 链表去重(C++)

给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉。即对每个键值 K,只有第一个绝对值等于 K 的结点被保留。同时,所有被删除的结点须被保存在另一个链表上。例如给定 L 为 21→-15→-15→-7→15,你需要输出去重后的链表 21→-15→-7,还有被删除的链表 -15→15。

输入格式:

输入在第一行给出 L 的第一个结点的地址和一个正整数 N(≤105,为结点总数)。一个结点的地址是非负的 5 位整数,空地址 NULL 用 -1 来表示。

随后 N 行,每行按以下格式描述一个结点:

地址 键值 下一个结点

其中地址是该结点的地址,键值是绝对值不超过104的整数,下一个结点是下个结点的地址。

输出格式:

首先输出去重后的链表,然后输出被删除的链表。每个结点占一行,按输入的格式输出。

输入样例:

00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854

输出样例:

00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1

代码:

#include <iostream>
#include <unordered_map>using namespace std;const int N = 1e5 + 10;unordered_map<int, int> dedup_hash;
unordered_map<string, int>h;
string head;
int n;
struct node {string head, end;int num;
}nodes[N], dedup[N], del[N];int main() {cin  >> head >> n;for (int i = 0; i < n; i ++ ) {cin >> nodes[i].head >> nodes[i].num >> nodes[i].end;h[nodes[i].head] = i;}string l = head, r = "-1";int l_cnt = 0, r_cnt = 0;int root = h[head];while(1) {if (!dedup_hash[abs(nodes[root].num)]) {dedup_hash[abs(nodes[root].num)] = 1;if (l_cnt != 0) dedup[l_cnt - 1].end = nodes[root].head;dedup[l_cnt].head = nodes[root].head;dedup[l_cnt].num = nodes[root].num;// dedup[l_cnt].end = "-1";l_cnt ++ ;}else {if (r_cnt != 0) del[r_cnt - 1].end = nodes[root].head;del[r_cnt].head = nodes[root].head;del[r_cnt].num = nodes[root].num;r_cnt ++ ;}if (nodes[root].end == "-1") break;root = h[nodes[root].end];}for (int i = 0; i < l_cnt; i ++ ) {if (i == l_cnt - 1) cout << dedup[i].head << ' ' << dedup[i].num << ' ' << "-1" << endl;else cout << dedup[i].head << ' ' << dedup[i].num << ' ' << dedup[i].end << endl;}for (int i = 0; i < r_cnt; i ++ ) {if (i == r_cnt - 1) cout << del[i].head << ' ' << del[i].num << ' ' << "-1" << endl;else cout << del[i].head << ' ' << del[i].num << ' ' << del[i].end << endl;}
}
http://www.lryc.cn/news/375825.html

相关文章:

  • 异或运算在面试题中的应用
  • 【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 单词大师(100分) - 三语言AC题解(Python/Java/Cpp)
  • LabVIEW在SpaceX的应用
  • 【Android面试八股文】讲一讲String、StringBuffer和StringBuilder在进行字符串操作时候的效率
  • [自动驾驶 SoC]-4 特斯拉FSD
  • PostgreSQL源码分析——物化视图
  • 操作系统入门系列-MIT6.828(操作系统工程)学习笔记(七)---- 系统调用函数与GDB(Lab: system calls)
  • ORA-12560: TNS:协议适配器错误
  • 不容小觑的“白纸黑字”:银行重空凭证的风险与防控
  • 30v-180V降3.3V100mA恒压WT5107
  • Spring Boot 和 Spring Cloud 的区别及选型
  • 【神经网络】图像的数字视角
  • ChatGPT的问题与回复的内容导出(Chorme)
  • 游戏开发中的坑之十四 photoshop的javascript脚本批量修改分辨率
  • leetcode打卡#day45 携带研究材料(第七期模拟笔试)、518. 零钱兑换 II、377. 组合总和 Ⅳ、爬楼梯(第八期模拟笔试)
  • Vite+Vue3安装且自动按需引入Element Plus组件库
  • 敬酒词大全绝对实用 万能敬酒词
  • 【Java】已解决com.mysql.cj.jdbc.exceptions.CommunicationsException异常
  • Leetcode 76. 最小覆盖子串
  • JAVAWEB--Mybatis03
  • 论文学习_Fuzz4All: Universal Fuzzing with Large Language Models
  • 元数据相关资料整理 metadata
  • 【Android面试八股文】谈一谈你对http和https的关系理解
  • Vue3 中 setup 函数与 script setup 用法总结
  • Springboot 开发之任务调度框架(一)Quartz 简介
  • 企业中面试算法岗时会问什么pytorch问题?看这篇就够了!
  • 【学习】程序员资源网址
  • 【3D模型库】机械三维模型库整理
  • 基于Python-CNN深度学习的物品识别
  • Qt | 简单的使用 QStyle 类(风格也称为样式)