本文同步发布在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. 2013VS快捷键

    VS2013常用快捷键: 1.回到上一个光标位置/前进到下一个光标位置  1)回到上一个光标位置:使用组合键“Ctrl + -”: 2)前进到下一个光标位置:“Ctrl + Shift + - ”. ...

  2. FFmpeg 的sws_getContext函数 、sws_scale函数

    FFmpeg里面的sws_scale库可以在一个函数里面同时实现:1.图像色彩空间转换:2.分辨率缩放:3.前后图像滤波处理. 其核心函数主要有三个: // 初始化sws_scalestruct Sw ...

  3. http头部解释

    If-Modified-Since,If-Node-Match,ETag,Last-Modified 1 属于 Request Headers的是:If-Modified-Since,If-Node- ...

  4. 洛谷【P1104】生日(选择排序版)

    题目传送门:https://www.luogu.org/problemnew/show/P1104 题目很简单,不过我是来讲选择排序的. 选择排序\((Selection sort)\)是一种简单直观 ...

  5. Java中的String数据类型

    本文主要是说明一些String数据类型的基本知识,有些杂乱,不过都是比较重要的东西,主要是参考了网上人的资料.原文网址:http://dev.yesky.com/91/2309091.shtml 主要 ...

  6. 纯js+html+css实现模拟时钟

    前几天没事写的个模拟时钟,代码仅供小白参考,大神请自动绕过. <!DOCTYPE html> <html lang="en"> <head> & ...

  7. js数组中常用的几个API

    1.push:从末尾添加数据项. 2.pop:从末尾去除数据项. 3.shift:从开始去除数据项 4.splice: splice(m,n) m:指开始删除的索引位置  n:值删除几项 splice ...

  8. hadoop自己写的最高温度程序源码

    package com.teset; import java.io.IOException; import java.util.StringTokenizer; import org.apache.h ...

  9. [hdu4734]F(x)数位dp

    题意:求0~f(b)中,有几个小于等于 f(a)的. 解题关键:数位dp #include<bits/stdc++.h> using namespace std; typedef long ...

  10. Flask07 Jinja2模板测试器、控制语句IF/FOR、变量/块 赋值、作用域、块级作用域

    1 测试器及其使用 在模板中的 {{}} 可以书写测试器,格式如下 {{ 变量 is 测试器名称  }} 1.1 在python中导入 Jinja2 的模板 from jinja2 import te ...