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 (<= 105) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

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 104, 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<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
typedef struct NODE{
int lt, rt;
int data;
int rank;
int valid;
NODE(){
valid = -; //分为3类。不重复的合法数据为1,重复的合法数据为0,非法数据为-1
}
}node;
node nds[];
map<int, int> mp;
bool cmp(node a, node b){
if(a.valid != b.valid)
return a.valid > b.valid;
else return a.rank < b.rank;
}
int main(){
int N, head;
scanf("%d%d", &head, &N);
int addr;
for(int i = ; i < N; i++){
scanf("%d", &addr);
nds[addr].lt = addr;
scanf("%d %d", &nds[addr].data, &nds[addr].rt);
}
int pt = head, index = ;
while(pt != -){
if(mp.count(abs(nds[pt].data)) == ){
mp[abs(nds[pt].data)] = ;
nds[pt].valid = ;
}else{
nds[pt].valid = ;
}
nds[pt].rank = index++;
pt = nds[pt].rt;
}
sort(nds, nds + , cmp);
int dele = -;
for(int i = ; i < index; i++){
if(nds[i].valid != ){
dele = i;
break;
}
}
if(dele == -)
dele = ;
for(int i = ; i < dele; i++){
if(i < dele - )
printf("%05d %d %05d\n", nds[i].lt, nds[i].data, nds[i + ].lt);
else printf("%05d %d -1\n", nds[i].lt, nds[i].data);
}
for(int i = dele; i < index; i++){
if(i < index - )
printf("%05d %d %05d\n", nds[i].lt, nds[i].data, nds[i + ].lt);
else printf("%05d %d -1\n", nds[i].lt, nds[i].data);
}
cin >> N;
return ;
}

总结:

1、在遍历合法链表时给每个节点编号,之后按照编号进行排序,既可保持链表顺序,又方便输出。

2、给链表节点打不同的标记,再利用排序sort的分类的特点,可以对链表的节点进行分类。

3、利用 map 来记录某个节点的data是否在之前存在过。

A1097. Deduplication on a Linked List的更多相关文章

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

  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_A1097#Deduplication on a Linked List

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

  4. PAT1097:Deduplication on a Linked List

    1097. Deduplication on a Linked List (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ...

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

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

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

  7. pat1097. Deduplication on a Linked List (25)

    1097. Deduplication on a Linked List (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ...

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

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

随机推荐

  1. if...else 小练习

    # 需求:猜年龄,可以让用户最多猜三次 age = 60 for i in range(3): guess = int(input("Input Age: ")) if guess ...

  2. Docker容器学习梳理 - Dockerfile构建镜像

    在Docker的运用中,从下载镜像,启动容器,在容器中输入命令来运行程序,这些命令都是手工一条条往里输入的,无法重复利用,而且效率很低.所以就需要一 种文件或脚本,我们把想执行的操作以命令的方式写入其 ...

  3. HTML5-indexedDB使用常见错误总结

    indexedDB使用过程中常常会出现以下错误: Failed to execute ‘createObjectStore’ on ‘IDBDatabase’: The database is not ...

  4. 《Metasploit渗透测试魔鬼训练营》第一章读书笔记

    第1章 魔鬼训练营--初识Metasploit 20135301 1.1 什么是渗透测试 1.1.1 渗透测试的起源与定义 如果大家对军事感兴趣,会知道各国军队每年都会组织一些军事演习来锻炼军队的攻防 ...

  5. LINUX内核分析第六周学习总结——进程的描述与创建

    LINUX内核分析第六周学习总结--进程的描述与创建 标签(空格分隔): 20135321余佳源 余佳源 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc ...

  6. Beta版发布说明

    我们的作品“校友聊”软件的最终版本于6月19日最终发布了,下面我们将对自己的产品进行介绍. 在使用之前,首先要进行用户注册,用户可以自行设置自己的账号,姓名,密码,签名,头像等信息,头像信息也可以在文 ...

  7. python 使用spaCy 进行NLP处理

    原文:http://mp.weixin.qq.com/s/sqa-Ca2oXhvcPHJKg9PuVg import spacy nlp = spacy.load("en_core_web_ ...

  8. Java源码--Array

    1. Arrays.asList() 该方法是将数组转化为List,需要注意以下几点: (1)该方法不适用于基本数据类型(byte,short,int,long,float,double,boolea ...

  9. jquery打印页面(jquery.jqprint)

    使用jquery进行打印时,所需js包:jquery-1.4.4.min.js.jquery.jqprint-0.3.js 但如果使用高版本的jquery(jquery-1.9.1.min.js)时, ...

  10. error loading midas.dll问题

    如果用的delphi在你的单元里用uses midaslib这个东西就可以把midas静态连接到你的程序楼上的也可以 在程序中使用winexec("regsvr32.exe midas.dl ...