PAT甲级——1097 Deduplication on a Linked List (链表)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given Lbeing 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (≤) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −.
Then N lines follow, each describes a node in the format:
Address Key Next
where Address is the position of the node, Key is an integer of which absolute value is no more than 1, and Next is the position of the next node.
Output Specification:
For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854
Sample Output:
00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1
题目大意:从头结点开始遍历一个链表,将遇到的节点的key的绝对值标记,若重复,则将此节点放到另一个链表里;举个例子,List 21→-15→-15→-7→15,答案是 21→-15→-7 和 -15→15 两个链表。
思路:链表的节点地址是5位整数,-1表示Null ,用数组存放链表。基础的链表删减操作(其实只要更改相应节点的next地址),用 pre 记录前一个节点的地址,addr 记录当前节点的地址,数组也好、set也好、map也好,设置一个容器用于标记已经出现过的abs( key )。
#include <iostream>
#include <unordered_map>
#include <vector>
#include <cmath>
#define MaxNum 100001
using namespace std; struct node {
int key, next = -;
}; vector <node> List(MaxNum);
unordered_map <int, bool> S; int main()
{
int head, N;
scanf("%d%d", &head, &N);
for (int i = ; i < N; i++) {
int addr;
scanf("%d", &addr);
scanf("%d%d", &List[addr].key, &List[addr].next);
}
int pre = head, addr = List[head].next, secHead = -, secAddr;
S[abs(List[head].key)] = true; while (addr != -) {
if (S[abs(List[addr].key)]) {
List[pre].next = List[addr].next;
if (secHead == -) {
secHead = addr;
secAddr = secHead;
List[secHead].next = -;
}
else {
List[secAddr].next = addr;
secAddr = addr;
List[secAddr].next = -;
}
addr = List[pre].next;
}
else {
S[abs(List[addr].key)] = true;
pre = addr;
addr = List[addr].next;
}
}
for (addr = head; addr != -; addr = List[addr].next) {
printf("%05d %d ", addr, List[addr].key);
List[addr].next == - ? printf("-1\n") : printf("%05d\n", List[addr].next);
}
for (secAddr = secHead; secAddr != -; secAddr = List[secAddr].next) {
printf("%05d %d ", secAddr, List[secAddr].key);
List[secAddr].next == - ? printf("-1\n") : printf("%05d\n", List[secAddr].next);
}
return ;
}
PAT甲级——1097 Deduplication on a Linked List (链表)的更多相关文章
- PAT 1097. Deduplication on a Linked List (链表)
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...
- PAT甲级——A1097 Deduplication on a Linked List
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...
- PAT Advanced 1097 Deduplication on a Linked List (25) [链表]
题目 Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplica ...
- PAT 1097 Deduplication on a Linked List[比较]
1097 Deduplication on a Linked List(25 分) Given a singly linked list L with integer keys, you are su ...
- PAT (Advanced Level) Practise - 1097. Deduplication on a Linked List (25)
http://www.patest.cn/contests/pat-a-practise/1097 Given a singly linked list L with integer keys, yo ...
- PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
- 【PAT甲级】1097 Deduplication on a Linked List (25 分)
题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...
- PAT (Advanced Level) 1097. Deduplication on a Linked List (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- 1097. Deduplication on a Linked List (25)
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...
随机推荐
- 唐诗掠影:基于词移距离(Word Mover's Distance)的唐诗诗句匹配实践
词移距离(Word Mover's Distance)是在词向量的基础上发展而来的用来衡量文档相似性的度量. 词移距离的具体介绍参考http://blog.csdn.net/qrlhl/artic ...
- Struts2 - 配置文件详解
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- Qt Quick中的信号与槽
在QML中,在Qt Quick中,要想妥善地处理各种事件,肯定离不开信号与槽,本博的主要内容就是整理Qt 中的信号与槽的内容. 1. 链接QML类型的已知信号 QML中已有类型定义的信号分为两类:一类 ...
- ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)
Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...
- ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)
Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...
- 【C++】标准库sort函数的自定义排序
自定义排序需要单独写一个compare函数 例1 LeetCode 056. Merge Intervals Given a collection of intervals, merge all ov ...
- Hive安装配置要点
官网下载安装包: 在Profile下面定义HIVE_HOME以及HADOOP_HOME,然后在PATH下面添加HOME/bin目录,用于在命令行直接敲beeline,hive即可执行命令: 需要在ha ...
- MySQL数据库服务器参数优化mycnf,16G内存8核CPU,
业务场景: 后台支持手机在线更新系统,db服务器内存16G,8核,dell的pc服务器. qps: 200个左右 tps: 1个左右 一分钟50几个 sort_buffer_size = 32M 大了 ...
- redis安装及启动及设置
1. 安装 1.1 下载解压包,直接解压到任意路径下即可 windows下载地址:ttps://github.com/MSOpenTech/redis/releases 2.启动 2.1 启动要先开启 ...
- Poj 1860 Currency Exchange(Bellman-Ford,SPFA解单源最短路径问题)
一.题意 有多个货币交易点,每个只能互换两种货币,兑换的汇率不同,并收取相应的手续费.有N种货币,假定你拥有第S中,数量为V,有M个兑换点.问你能不能通过兑换操作使你最后拥有的S币比起始的时候多. 二 ...