本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982

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 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 (链表)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作

    给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...

  7. 【PAT甲级】1097 Deduplication on a Linked List (25 分)

    题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...

  8. PAT (Advanced Level) 1097. Deduplication on a Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  9. 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 ...

随机推荐

  1. 截取URL参数的方法

    1,有点小瑕疵,双问号会截取不到第一个参数 function GetQueryString(name){ var reg = new RegExp("(^|&)"+ nam ...

  2. LiveMediaStreamer

    LiveMediaStreamer is an open source multimedia framework that allows the manipulation of multiple au ...

  3. Agc018_B Sports Festival

    传送门 题目大意 有$n$个人,$m$种运动$(n,m\leq 300)$,每个人对$m$种运动有喜爱度的排名. 请你划分一个$m$种运动的非空集合,使得当每个人参加集合内喜爱度排名最高的运动时,最多 ...

  4. IronPython 源码剖析系列(1):IronPython 编译器

    自 IronPython 正式发布以来,由于对 Python 语言的喜爱所驱使,同时我想藉此去了解一下编程语言的编译器,分析器等程序是什么原理,如何运作的,所以我开始了对 IronPython 源代码 ...

  5. 标准模板库(STL)学习指南之set集合

    set是关联容器.其键值就是实值,实值就是键值,不可以有重复,所以我们不能通过set的迭代器来改变set的元素的值,set拥有和list相同的特性:当对他进行插入和删除操作的时候,操作之前的迭代器依然 ...

  6. UILabel UiButton 文字下面加下划线

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"直接进入" ...

  7. wpf dataGrid 选中行 失去焦点时 的背景颜色的更改

    关于 wpf dataGrid 选中行 失去焦点时 的背景颜色的更改.很简单的方式,在datagrid的resource中更改InactiveSelectionHighlightBrushKey属性的 ...

  8. 【转】js中select的基本操作

    判断select选项中 是否存在Value="paraValue"的Item  // 1.判断select选项中 是否存在Value="paraValue"的I ...

  9. jquery 图片轮换

    jquery 图片轮换 1.下载jquery.superslide.2.1.1.js (百度搜索) 2.下载Jquery-1.4.1.js(百度搜索下载) 准备工作好了,下面开始实现 3.html & ...

  10. LAMP 1.1 Mysql

    1.下载软件包                                                                                             ...