PAT_A1097#Deduplication on a Linked List
Source:
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, andNext
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的更多相关文章
- PAT1097:Deduplication on a Linked List
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 ...
- pat1097. Deduplication on a Linked List (25)
1097. Deduplication on a Linked List (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ...
- 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 ...
随机推荐
- Python中的浮点数原理与运算分析
Python中的浮点数原理与运算分析 本文实例讲述了Python中的浮点数原理与运算.分享给大家供大家参考,具体如下: 先看一个违反直觉的例子: >>> s = 0. > ...
- 关于Java中编码集的有趣现象和解释
这是在整理另一篇博客的时候发现的一个有趣的现象,是这样描述的:我们都知道Java默认使用的是UniCode编码集,我们也知道char类型占用两个字节.所以奇怪的现象又发生了(见代码): @Test p ...
- https中引用http
https里引用http不安全,会报错 解决方案 1.可以部署在http中,http中引用https不会存在安全问题报错 2.https中引用https,把需要引用的http部署成https
- POJ 1860 Currency Exchange (Bellman-Ford)
题目链接:POJ 1860 Description Several currency exchange points are working in our city. Let us suppose t ...
- 【Mock.js】前端模拟假数据,不用在手拼了
[Mock.js]前端模拟假数据,不用在手拼了:https://www.jianshu.com/p/8579b703a4c1
- luoguP2184 贪婪大陆 题解(树状数组)
P2184 贪婪大陆 题目 其实很容易理解就是询问一段区间内有多少段不同的区间 然后再仔细思索一下会发现: 1.只要一个区间的开头在一个节点i的左边,那么这个区间包含在区间1~i中. 2.只要一个区 ...
- APACHE两种域名跳转法简单完成重定向
当我们变更网站域名,或者申请多个域名指向一个网站的时候,这个时候我们就会用到域名跳转(或者叫域名重定向redirect.域名转向).下面用最简单的文字讲两种apache的域名跳转方法. 假设我们想把w ...
- Windows server 2016远程桌面登录和修改3389端口
- 52-python基础-python3-列表-常用列表方法- reverse()方法
reverse()方法 永久性地修改列表元素的排列顺序,但可随时恢复到原来的排列顺序,为此只需对列表再次调用reverse() 即可. 实例:
- 37.Sort Colors(颜色排序)
Level: Medium 题目描述: Given an array with n objects colored red, white or blue, sort them in-place s ...