PAT1097:Deduplication on a Linked List
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 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的更多相关文章
- pat1097. Deduplication on a Linked List (25)
1097. Deduplication on a Linked List (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ...
- 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 ...
- PAT甲级——1097 Deduplication on a Linked List (链表)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...
- PAT_A1097#Deduplication on a Linked List
Source: PAT A1097 Deduplication on a Linked List (25 分) Description: Given a singly linked list L wi ...
- 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 ...
- 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 ...
- A1097. Deduplication on a Linked List
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...
- 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 ...
- 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 ...
随机推荐
- Hive 配置
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="confi ...
- React Native ios开发第一课
前言 本篇文章的作用在于帮助你快速上手使用React Native编写iOS应用.如果你现在还不太了解React Native是什么以及Facebook为什么要创建React Native,你可以先看 ...
- JNI设置C++与java的结合(2)
我们可以看到其中有四个函数声明, Java_完整类名_方法名, 完整类名包括了包名, 例如demo.Sample1是完整类名, 对应的这里就是demo_Sample1. 在注释中我们可以看到这样一个东 ...
- ubuntu系统AndroidStudio修改内存大小
位于android-studio/bin目录下的studio64.vmoptions和studio.vmoptions文件. 把Xms,Xmx,-XX:MaxPermSize,-XX:Reserved ...
- LeetCode之“链表”:Add Two Numbers
题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...
- C语言之选择排序
选择法排序是相对好理解的排序算法.假设要对含有n个数的序列进行升序排列,算法步骤是: 1.从数组存放的n个数中找出最小数的下标(算法见下面的"求最值"),然后将最小数与第1个数交换 ...
- Visual studio2010和Modelsim配置SystemC开发(转)
本文转自一博文. 一.编译System库 1. 下载SystemC library source code, 到http://www.systemc.org注册会员账号后,即可下载SystemC li ...
- HBase Master 启动
–>首先初始化HMaster –>创建一个rpcServer,其中并启动 –>启动一个Listener线程,功能是监听client的请求,将请求放入nio请求队列,逻辑如下: –&g ...
- 基于opencv的gpu与cpu对比程序,代码来自opencv的文档中
原文链接: http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/gpu/gpu-basics-similarity/gpu-basi ...
- HBase 协处理器实现二级索引
HBase在0.92之后引入了coprocessors,提供了一系列的钩子,让我们能够轻易实现访问控制和二级索引的特性.下面简单介绍下两种coprocessors,第一种是Observers,它实际类 ...