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)的更多相关文章

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

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

  3. PAT (Advanced Level) 1097. Deduplication on a Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  4. PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作

    给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...

  5. 【PAT甲级】1097 Deduplication on a Linked List (25 分)

    题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...

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

  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. 整合iis+tomcat

    目的: 将 Tomcat与 IIS整合在一起,共用 80端口.让 iis可以解析 *.asp. *.aspx. *.jsp. servlet和 *.do文件: 第一步:准备工作. 在你的 Tomcat ...

  2. Frameset使用教程 小结

    frame,是网页开发必须掌握的知识.例如后台架构.局部刷新,页面分割,都是frame的用途表现,尤其是后台页面制作,使用frame会给用户带来非常舒适的使用感受. frame知识点包括(frames ...

  3. Java操作文件夹的工具类

    Java操作文件夹的工具类 import java.io.File; public class DeleteDirectory { /** * 删除单个文件 * @param fileName 要删除 ...

  4. hdu1251

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  5. MarkDown认识与入门

    Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdown 的语法十分简单.常用的标记符号也不 ...

  6. 获取动态SQL的返回结果

    1. 介绍说明 有时候在执行存储过程后,需要获取存储过程返回的列表,然后进行相应操作的情况,或者执行动态语句,获取返回结果的情况,通过EXEC ,sp_executesql可以实现该功能. 网上也有很 ...

  7. PHP浮点数的精度

    在百度知道上看到这么一个问题 var_dump((0.3-0.2)==0.1); 结果是:false 后来查查手册,原来是浮点数的精度问题.那么0.3-0.2-0.1等于多少呢,结果:2.775557 ...

  8. 【奇怪现象】用联通访问某些ASP.NET网站会产生__EVENTVALIDATION字段,用电信却只有:__VIEWSTATE。【正常】?原因?

    [奇怪现象]用联通访问某些ASP.NET网站会产生__EVENTVALIDATION字段,用电信却只有:__VIEWSTATE.[正常]?原因? 对于__VIEWSTATE和__EVENTVALIDA ...

  9. Android OpenGL ES(三)----编程框架

    首先当然是创建Android项目,你可以选择最新的Android Studio也可以选择eclipse都是一样的.我们重点讲解开发OpenGL ES的流程 1.定义顶点着色器和片段着色器 第一节我们讲 ...

  10. Oracle数据库对象_视图

    视图是一种非常重要的数据库对象,它的形式类似于普通表,我们可以从视图中查询数据. 实际上它是建立在表上的一种虚表,在视图中并不存储真正的数据,而是仅仅保存一条SELECT语句,对视图的访问将被转化为对 ...