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 L being 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
 #include <iostream>
#include <unordered_map>
#include <cmath>
using namespace std;
struct Node
{
int addr, val;
Node(int a, int b) :addr(a), val(b) {}
};
struct List
{
Node val;
List* next;
List(Node a) :val(a), next(nullptr) {}
};
int N, head, addr1, addr2, val, numbers[] = { };
int main()
{
cin >> head >> N;
if (head == -)//切记,此时有一个测试例子是头结点为-1,那么什么也不输出
return ;
unordered_map<int, pair<int, int>>map;
for (int i = ; i < N; ++i)
{
cin >> addr1 >> val >> addr2;
map[addr1] = make_pair(val, addr2);
}
//将链表组合起来
List* resHead = new List(Node(, -));
List* delHead = new List(Node(, -));
List* p = resHead;
while (head != -)
{
List* q = new List(Node(head, map[head].first));
p->next = q;
p = q;
head = map[head].second;
}
List* pre = resHead, *delP = delHead;
p = pre->next;
while (p != nullptr)
{
if (numbers[abs(p->val.val)] == )
{
pre->next = p->next;//删除
List* q = new List(Node(p->val.addr, p->val.val));
delP->next = q;
delP = q;
delete p;
p = pre->next;
}
else
{
numbers[abs(p->val.val)]++;
pre = p;
p = pre->next;
}
}
p = resHead->next;
while (p != nullptr)
{
printf("%05d %d ", p->val.addr, p->val.val);
p = p->next;
if (p == nullptr)
printf("%d\n", -);
else
printf("%05d\n", p->val.addr);
}
p = delHead->next;
while (p != nullptr)
{
printf("%05d %d ", p->val.addr, p->val.val);
p = p->next;
if (p == nullptr)
printf("%d\n", -);
else
printf("%05d\n", p->val.addr);
}
return ;
}

PAT甲级——A1097 Deduplication on a Linked List的更多相关文章

  1. PAT甲级——1097 Deduplication on a Linked List (链表)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...

  2. PAT A1097 Deduplication on a Linked List (25 分)——链表

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

  3. A1097. Deduplication on a Linked List

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

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

  5. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  6. PAT_A1097#Deduplication on a Linked List

    Source: PAT A1097 Deduplication on a Linked List (25 分) Description: Given a singly linked list L wi ...

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

  8. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

  9. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. Redis连不上的一些细节配置

    远程链接redis连不上,在确保防火墙设置正确的情况下 把redis.conf中的 bind 127.0.0.1注释 另外把protected-mode yes 改为protected-mode no

  2. element-ui的layout将24等分换为48等分

    按住ctr箭点击element-ui/packages/theme-chalk/src/index";,再按住ctr贱点击col.scss跳转,将跳转到的col.scss中的24换为48(c ...

  3. Linux 常用命令:文本查看篇

    前言 Linux常用命令中,除了cat还有很多其他用于文本查看的命令.本文将简单介绍一下这些文本查看的命令. 全文本显示--cat cat可能是常用的一个文本查看命令了,使用方法也很简单: cat f ...

  4. 2019牛客暑期多校训练营(第八场) E 线段树+可撤销并查集

    题目传送门 题意: 给出m条无向边,每条边都有一个$[l,r]$,意思是体积在这个范围内的人才能通过这条边,询问有多少种体积的可能性,能使人从1到n 思路:由于是无向边,1和n的连通性可以用并查集维护 ...

  5. CF773E Blog Post Rating

    题意:有一篇博客.一共有n个人,心中有他们期望该博客得到的赞数a[i].当某个时刻该博客的获赞数<a[i],则该人会使得赞数+1,当赞数>a[i],该人会使得赞数-1,当赞数=a[i],不 ...

  6. 手写代码注意点 -- HashMap

    1.定义 HashMap<String,String> hashMap = new HashMap<>(); <String,String>只需要写一遍 2.获取k ...

  7. Java导出pdf文件数据

    提示:导出pdf文件,需要3个jar包iText-2.1.5.jar,iTextAsian.jar,iText-rtf-2.1.4.jar. public boolean outputPdfJhsy( ...

  8. nginx反向代理时有无”/”的辨析

    nginx反向代理是日常使用nginx时最常用到的功能之一.在配置url的过程中,“/”的有无经常是影响我们配置成功的关键,也是困扰我们的问题所在.在此,结合实际例子,作简要辨析. 场景一:利用根目录 ...

  9. 二分图带权匹配-Kuhn-Munkres算法模板 [二分图带权匹配]

    尴尬...理解不太好T T #include<cstdio> #include<cstring> #include<iostream> #include<al ...

  10. hdu多校第二场 1010 (hdu6600)Just Skip This Problem

    题意: 给你一个数x,允许你多次询问yi,然后回答你x xor yi 是否等于yi,询问尽量少的次数以保证能求出xi是几,求出这样询问次数最少的询问方案数. 结果mod1e6+3 题解: 队友赛时很快 ...