1097. Deduplication on a Linked List (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 思路
1.map模拟一个链表dic存放输入的所有的节点,set模拟一个dupdic记录一个节点的值来判断接下来的节点是否由重复的key值,两个vector分别为去重链表和重复节点链表。
2.遍历map,根据set容器内的key值决定是将当前节点插入去重链表还是重复节点链表,并不断更新set存放的key值。
3.对两个vector新链表的节点的next地址赋予它下个节点的地址。
4.遍历两个链表并依次输出即可,
代码
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<math.h>
using namespace std;
class node
{
public:
int address;
int key;
int next;
}; map<int,node> dic;
set<int> dupdic;
vector<node> newlist;
vector<node> removed_list; int main()
{
int head,n;
while(cin >> head >> n)
{
//input
for(int i = 0;i < n;i++)
{
int adr;
cin >> adr;
dic[adr].address = adr;
cin >> dic[adr].key >> dic[adr].next;
}
//handle
while(head != -1)
{
if(dupdic.find(abs(dic[head].key)) != dupdic.end())
{
removed_list.push_back(dic[head]);
}
else
{
newlist.push_back(dic[head]);
dupdic.insert(abs(dic[head].key));
}
head = dic[head].next;
}
int lenn = newlist.size(),lenr = removed_list.size();
//output
for(int i = 1;i <= lenn;i++)
{
if(i == lenn)
{
newlist[i - 1].next = -1;
printf("%05d %d %d\n",newlist[i - 1].address,newlist[i - 1].key,newlist[i - 1].next);
}
else
{
newlist[i - 1].next = newlist[i].address;
printf("%05d %d %05d\n",newlist[i - 1].address,newlist[i - 1].key,newlist[i - 1].next);
}
}
for(int i = 1;i <= lenr;i++)
{
if(i == lenr)
{
removed_list[i - 1].next = -1;
printf("%05d %d %d\n",removed_list[i - 1].address,removed_list[i - 1].key,removed_list[i - 1].next);
}
else
{
removed_list[i - 1].next = removed_list[i].address;
printf("%05d %d %05d\n",removed_list[i - 1].address,removed_list[i - 1].key,removed_list[i - 1].next);
}
}
}
}

  

PAT1097:Deduplication on a Linked List的更多相关文章

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

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

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

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

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

  4. PAT_A1097#Deduplication on a Linked List

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

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

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

  7. A1097. Deduplication on a Linked List

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

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

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

随机推荐

  1. Oracle Inventory Management Application Program Interface ( APIs)

    In this Document   Goal   Solution   References APPLIES TO: Oracle Inventory Management - Version 12 ...

  2. 【Android 应用开发】Android 数据存储 之 SQLite数据库详解

    . 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...

  3. Linux用户配置文件(第二版)

    /etc/passwd文件剖析 文件格式: root:x:0:0:root:/root:/bin/bash 用户名:密码位:UID:GID[缺省组ID]:注释性的描述信息:宿主目录:shell[7部分 ...

  4. Learning ROS for Robotics Programming Second Edition学习笔记(三) indigo rplidar rviz slam

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...

  5. TCP的核心系列 — SACK和DSACK的实现(六)

    上篇文章中我们主要说明如何skip到一个SACK块对应的开始段,如何walk这个SACK块包含的段,而没有涉及到 如何标志一个段的记分牌.37版本把给一个段打标志的内容独立出来,这就是tcp_sack ...

  6. HBase flush

    flush触发方式 1. Server端执行更新操作(put.delete.multi(MultiAction<R>multi).(private)checkAndMutate.mutat ...

  7. 面试之路(28)-反转链表(reverse ListNode)

    反转链表: java类 public class ListNode{ int key; ListNode next; } 思路分析: 需要三个指针,current,prev和next. current ...

  8. SharePoint 2010 之寻找页面布局

    习惯了2007的页面布局,虽然感觉不是太好用,尤其以开始接触时非常不理解页面布局和页面的关系,但是后来理清了,感觉还是很好用的,尤其对于相同格式的网站,修改布局而不改页面的情况,还是非常有效的,好了, ...

  9. rails将类常量重构到数据库对应的表中之二

    在博文之一中我们将Order中的常量重构到了数据库的表中,也做了一些测试,貌似一切都很完美.可是...梦魔还未开始啊!我们少做了一步测试,就是rake test! 结果惨不忍睹,所有测试都是E,全部出 ...

  10. mysql-proxy中的admin-lua-script

    [root@ecs-7b55 lua]# cat admin.lua --[[ $%BEGINLICENSE%$ Copyright (c) 2008, 2012, Oracle and/or its ...