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
#include<stdio.h>
#include<math.h>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
int val;
int next,id;
};
node Link[];
int main()
{
int head,n,id,val,next;
scanf("%d%d",&head,&n);
for(int i = ;i < n ;++i)
{
scanf("%d%d%d",&id,&val,&next);
Link[id].val = val;
Link[id].next = next;
Link[id].id = id;
}
int p = head;
set<int> ss;
vector<node> vv,vv2;
while(p != -)
{
int tem = abs(Link[p].val);
if(ss.count(tem) == )
{
ss.insert(tem);
vv.push_back(Link[p]);
}
else vv2.push_back(Link[p]);
p = Link[p].next;
}
p = head;
for(int i = ; i < (int)vv.size() - ;++i)
{
printf("%05d %d %05d\n",vv[i].id,vv[i].val,vv[i+].id);
}
if(vv.size() > )
printf("%05d %d -1\n",vv[vv.size()-].id,vv[vv.size()-].val); for(int i = ;i < (int)vv2.size() -;++i)
{
printf("%05d %d %05d\n",vv2[i].id,vv2[i].val,vv2[i+].id);
}
if(vv2.size() > )
printf("%05d %d -1\n",vv2[vv2.size()-].id,vv2[vv2.size()-].val);
return ;
}
1097. Deduplication on a Linked List (25)的更多相关文章
- 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 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 ...
- PAT (Advanced Level) 1097. Deduplication on a Linked List (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
- 【PAT甲级】1097 Deduplication on a Linked List (25 分)
题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...
- PAT甲级——1097 Deduplication on a Linked List (链表)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...
- 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 (链表)
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...
随机推荐
- 《MFC游戏开发》笔记五 定时器和简单动画
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9332377 作者:七十一雾央 新浪微博:http:// ...
- (一)u-boot2013.01.01 for TQ210:《Uboot简介》
一直想写一个s5pv210硬件平台的u-boot的移植文档,但一直都忙着没时间写.先写一些u-boot的脚本分析吧,包括makefile,mkconfig,config.mk,主要侧重于语法句意的分析 ...
- 关于Java中获取当前系统时间
一. 获取当前系统时间和日期并格式化输出: import java.util.Date; import java.text.SimpleDateFormat; public class NowStri ...
- mount: unknown filesystem type 'LVM2_member'解决方案
系统启动到request_module: runaway loop modprobe binfmt-464c挂起 利用U盘系统,挂载硬盘出现:mount: unknown filesystem typ ...
- CSS3--动态实现ToolTip效果(实例)
效果图如下↓↓↓↓↓ (知识点见代码注释) HTML <!DOCTYPE html> <html lang="en"> <head> < ...
- 破解软件系列-PE文件深入浅出之Section Table节表
我们已经学了许多关于 DOS header 和 PE header 的知识.接下来就该轮到 section table(节表)了.节表其实就是紧挨着 PE header 的一结构数组.该数组成员的数目 ...
- Part 45 to 47 Talking about Enums in C#
Part 45 C# Tutorial Why Enums Enums are strongly typed constants. If a program uses set of integ ...
- wamp图标黄色的另一种可能原因
学习php用的是wamp即windows+apache+mysql+php,但是当启动的时候,图标为黄色(正常应为绿色),开始以为是端口的问题,但是将端口改掉依旧如此,后来发现是机器装的sqlserv ...
- xenserver xensource.log不断增长
转载:http://blog.sina.com.cn/s/blog_4ca83f830100xded.html 相信很多人被Xenserver日志填满磁盘空间,导致机器最终挂掉的问题所困扰,我 ...
- 使用命令修改ip地址
简述:以serverv 2012 r2为例 常用的几种,当然不全,希望能较快的速率记下一种便可 直接配置 1. 查看网卡的显示名称 2. 配置静态iP地址 3. 查看配置 ...