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. uva 10474 Where is the Marble? 计数排序

    题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...

  2. Oracle 基础 <2> --函数

    一:函数的定义 函数是用于返回特定数据的PL/SQL程序块 (函数必须返回一个值) 语法: create [or replace] function function_name--函数名称 [(par ...

  3. IOS学习网站

    http://www.cocoachina.com http://onevcat.com/#blog http://objccn.io http://blog.devtang.com http://b ...

  4. oracle查看允许的最大连接数和当前连接数等信息

    目前总结的语句,在查看数据的连接情况很有用 ,写完程序一边测试代码一边查看数据库连接的释放情况有助于分析优化出一个健壮的系统程序来. 1.Sql代码1.select count(*) from v$p ...

  5. 第二章 Background & Borders 之 Multiple borders

    2. Multiple Boerders 多边框 在工作中我们可能会遇到给盒子外层实现多个边框.如以下效果: 方法1: 实现这个效果,其实很简单,使用CSS3 的box-shadow属性,先看看box ...

  6. Table of Contents - TCP/IP

    网络访问层 Ethernet II 网际层 IP IPv4 报文格式 ICMP ICMP 报文分析 ping: 向网络主机发送 ICMP ECHO_REQUEST 包 ARP ARP 过程 arp 命 ...

  7. 转:C#写的WEB服务器

    转:http://www.cnblogs.com/x369/articles/79245.html 这只是一个简单的用C#写的WEB服务器,只实现了get方式的对html文件的请求,有兴趣的朋友可以在 ...

  8. AjaxPro框架

    AjaxPro框架 本章主要了解Ajax框架,掌握Ajaxpro框架的配置以及应用. 内容如下,请点击AjaxPro框架查看:

  9. shell获取本地ip的三种方法

    第一种方法:ifconfig|grep inet |awk '{print $2}'|sed '2d'|awk -F : '{print $2}'第二种方法:ifconfig|grep inet|se ...

  10. C#中Messagebox.Show()常用参数用法详解

    声明:IWin32Window owner   ,  HelpNavigator navigator ,    string keyword 上面的三个参数类型不是很了解.没有做讨论. 等以后了解多了 ...