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
Addressis the position of the node,Keyis an integer of which absolute value is no more than 1, andNextis 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 ...
随机推荐
- HEAD请求
平时用的最多的无外乎POST GET 很少用的HEAD 这次HEAD请求使用场景:判断资源是否存在
- electron测试TCP通信
这几天学习了一下Elctron,对于这个应用有了一点简单的认识,将这个过程记录一下. 首先,electron会加载main.js,在这里将整个程序启动,相当于其他程序的main函数了. 我是基于ele ...
- java多线程学习笔记(八)
本节开始线程间通信: 使用wait/notify实现线程间通信 生产者/消费者模式的实现 方法join的使用 ThreadLocal类的使用 可以通过使用 sleep() 结合 while(true) ...
- SpringMVC上传文件的三种方式(转帖)
/* * 通过流的方式上传文件 * @RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象 */ @Re ...
- HTML创建文本框的3种方式
我的第一个随笔,记录主要用来整理学习的知识点 1.input 创建单行文本框 <input type="text" size="10" maxlength ...
- c++实现哈夫曼树,哈夫曼编码,哈夫曼解码(字符串去重,并统计频率)
#include <iostream> #include <iomanip> #include <string> #include <cstdlib> ...
- thinkphp中的exp查询
今天遇到一个问题,就是在vendor表中查询出vendor_id = vendor_f_id的数据,其实使用原生的sql语句是非常简单的: select * from vendor where ven ...
- Web 请求之--性能相关
本博客代码运行环境 python : Python 3.7.1rc1 version pip : pip 19.1.1 version Scrapy: scrapy 1.6.0 version asy ...
- Debug和Release区别(转)
地址:https://zhidao.baidu.com/question/629188090208609884.html 最近写代码过程中,发现 Debug 下运行正常,Release 下就会出现问题 ...
- CNN基础一:从头开始训练CNN进行图像分类(猫狗大战为例)
本文旨在总结一次从头开始训练CNN进行图像分类的完整过程(猫狗大战为例,使用Keras框架),免得经常遗忘.流程包括: 从Kaggle下载猫狗数据集: 利用python的os.shutil库,制作训练 ...