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. JavaScript进阶(四)js字符串转换成数字的三种方法

    js字符串转换成数字的三种方法 在js读取文本框或者其它表单数据的时候获得的值是字符串类型的,例如两个文本框a和b,如果获得a的value值为11,b的value值为9 ,那么a.value要小于b. ...

  2. java的参数传递与内存分配问题

    本文可作为北京尚学堂java课程的学习笔记. 看下面这段代码. class BirthDate { private int day; private int month; private int ye ...

  3. 【面试笔试算法】Problem 9: 腾讯2016年研发实习笔试题:最长回文子串

    (一)题目 问题:求给定字符串s的回文(palindrome)子串中,长度最大的回文子串的长度. 回文(palindrome)是指从左往右读和从右往左读字符串,看到的字符串都是一样的.比如" ...

  4. Unix/Linux中的grep命令(转)

    本文转载自:如何使用Unix/Linux grep命令——磨刀不误砍柴工系列.该博文条理很清晰. grep简介 grep在一个或多个文件中查找与模式字符串(pattern)匹配的行,并将搜索的结果打印 ...

  5. 三消游戏FSM状态机设计图

    三消游戏FSM状态机设计图 1) 设计FSM图 2) smc配置文件 ///////////////////////////////////////////////////////////////// ...

  6. LIRe 源代码分析 6:检索(ImageSearcher)[以颜色布局为例]

    ===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...

  7. 在Windows使用git工具将代码同步至github(作者:ying1989920)

     [ps]git是一个分布式代码管理工具,类似于svn,方便协同开发,git里面有所谓的仓库(用来存放代码的),分为本地和线上,线上的你可以自己搭建,不想搭建的话github就给你提供了. [关于 ...

  8. 面试之路(13)-android apk之间共享数据的方式以及shareUserId详解

    1.通过content Provider/sharedPreferrence 2.通过shareUserId 我们详细介绍一下shareUserId: Android App Sandbox(andr ...

  9. 图文并茂的生产者消费者应用实例demo

    前面的几篇文章<<.NET 中的阻塞队列BlockingCollection的正确打开方式>><<项目开发中应用如何并发处理的一二事>>从代码以及理论角 ...

  10. 解析Json字符串的三种方法

    在很多时候,我们的需要将类似 json 格式的字符串数据转为json, 下面将介绍日常中使用的三种解析json字符串的方法 1.首先,我们先看一下什么是 json 格式字符串数据,很简单,就是 jso ...