Source:

PAT A1097 Deduplication on a Linked List (25 分)

Description:

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 (≤) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −.

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

Keys:

  • 哈希映射

Attention:

  • 注意keep和remo为空时,不能输出-1

Code:

 #include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
const int M=1e5+;
int mp[M]={};
struct node
{
int data;
int adrs,rear;
}link[M],t; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int first,n;
scanf("%d%d", &first,&n);
for(int i=; i<n; i++)
{
scanf("%d%d%d", &t.adrs,&t.data,&t.rear);
link[t.adrs] = t;
}
vector<int> keep,remo;
while(first != -)
{
if(mp[(int)abs(link[first].data)]==)
{
keep.push_back(first);
mp[(int)abs(link[first].data)]=;
}
else
remo.push_back(first);
first = link[first].rear;
}
for(int i=; i<keep.size(); i++)
{
if(i!=)
printf("%05d\n", keep[i]);
printf("%05d %d ", keep[i], link[keep[i]].data);
}
if(keep.size())
printf("-1\n");
for(int i=; i<remo.size(); i++)
{
if(i!=)
printf("%05d\n", remo[i]);
printf("%05d %d ", remo[i], link[remo[i]].data);
}
if(remo.size())
printf("-1\n"); return ;
}

PAT_A1097#Deduplication on a Linked List的更多相关文章

  1. PAT1097:Deduplication on a Linked List

    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. pat1097. Deduplication on a Linked List (25)

    1097. Deduplication on a Linked List (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ...

  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. .net 运行原理

    刚学习那会,感觉.net运行原理是很复杂的,也去了解过相关的东西,但是很晦涩,难于理解.感觉有些难了,也就放弃了解了.今天回头想想,也是当时有些毛躁了,不管怎么说,时至今日是有些明白运行原理. 从头开 ...

  2. 禁止修改input内容

    有什么问题请到<a href='/bbs/index.asp?boardid=2'>论坛</a>中发表<br> <!--# 特效来源:http://www.o ...

  3. javascript中call(),apply()用法

    ​ //上下文模式:根据用户传递的参数产生不同的结果 //实现方式:call/apply:这两个都是定义在Function.prototype.call——>目的:任何函数都可以访问到call/ ...

  4. Django-自定义用户模型

    Django最方便的一点,是自带用户系统,但有些情况下,不符合项目需求, 原因1.我们有时候需要自定义一些字段,或者删除Django自带字段,2.我们有时候需要定义哪些字段是必填的,登陆时的用户名是哪 ...

  5. springboot 尚桂谷学习总结02

    ------向导快速创建Springboot 项目------ 1.使用spring initializer 快速创建一个springboot 项目 选择后 最后点击finsh 向导会联网创建 spr ...

  6. s11 day106-107 RBAC模块

    一.登录 把权限存在session中 1. rbac models from django.db import models class Permission(models.Model): " ...

  7. HBase–RegionServer宕机恢复原理

    Region Server宕机总述 HBase一个很大的特色是扩展性极其友好,可以通过简单地加机器实现集群规模的线性扩展,而且机器的配置并不需要太好,通过大量廉价机器代替价格昂贵的高性能机器.但也正因 ...

  8. PAT 2019-3 7-2 Anniversary

    Description: Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for ...

  9. 记录java

    1.从今天起,我会将自己在java学习道路上的一些心得体会记录下来.

  10. jQuery设置checkbox 为选中状态

    1设置第一个checkbox 为选中值$('input:checkbox:first').attr("checked",'checked');或者$('input:checkbox ...