pat1097. Deduplication on a Linked List (25)
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 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<cstdio>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
using namespace std;
struct ndoe{
int k,next;
};
ndoe mem[];
bool ha[];
int abs(int a){
if(a<){
a=-a;
}
return a;
}
#define refirst 100001
#define nrfirst 100002
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,first;
scanf("%d %d",&first,&n);
int i,cur;
for(i=;i<n;i++){
scanf("%d",&cur);
scanf("%d %d",&mem[cur].k,&mem[cur].next);
}
cur=nrfirst;
int relist=refirst,p=first;
while(p!=-){
while(p!=-&&ha[abs(mem[p].k)]){
mem[relist].next=p;
relist=p;
p=mem[p].next;
}
if(p!=-){
ha[abs(mem[p].k)]=true;
}
mem[cur].next=p;
if(p!=-){
cur=p;
p=mem[cur].next;
}
}
mem[relist].next=-;
p=mem[nrfirst].next;
while(p!=-){
if(mem[p].next!=-){
printf("%05d %d %05d",p,mem[p].k,mem[p].next);
}
else{
printf("%05d %d %d",p,mem[p].k,mem[p].next);
}
printf("\n");
p=mem[p].next;
}
p=mem[refirst].next;
while(p!=-){
if(mem[p].next!=-){
printf("%05d %d %05d",p,mem[p].k,mem[p].next);
}
else{
printf("%05d %d %d",p,mem[p].k,mem[p].next);
}
printf("\n");
p=mem[p].next;
}
return ;
}
pat1097. Deduplication on a Linked List (25)的更多相关文章
- PAT1097:Deduplication on a Linked List
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 (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 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 ...
- PAT (Advanced Level) 1097. Deduplication on a Linked List (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
- 【PAT甲级】1097 Deduplication on a Linked List (25 分)
题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...
- PAT甲级——1097 Deduplication on a Linked List (链表)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...
- PAT_A1097#Deduplication on a Linked List
Source: PAT A1097 Deduplication on a Linked List (25 分) Description: Given a singly linked list L wi ...
随机推荐
- uboot和内核分区的修改
随着内核的更新,内核越来越大,uboot给nand的kernel分区默认是2M的 device nand0 <nandflash0>, # parts = 4 #: name ...
- SVN使用技巧和参考文档总结
以下文章为网上收集: myEclipse 8.5下SVN环境的搭建(重点推荐) SVN建立版本库,配置用户和权限 Tortoise SVN使用方法,简易图解 版本控制软件SVN使用方法详解 学习笔记 ...
- ssh动态转发小记
ssh,一般常用来做远程登录管理,也就是连上远程机器,得到一个shell,然后交互式地在上面敲命令-看结果-再敲命令. 偶尔也会用在脚本里,做些自动化批处理上传下载的操作,但本质上也是用shell来执 ...
- kafka之c接口常用API------librdkafka
1 安装方法以及相关库文件 https://github.com/edenhill/librdkafka 2 High-level producer High-level consumer Simpl ...
- Struts学习第一课 使用Filter作为控制器的MVC应用
MVC设计模式概览 实现MVC(Model,View,Controller)模式的应用程序由3大部分构成: -模型:封装应用程序的数据和业务逻辑(POJO,Plain Old Java Object) ...
- Free Style Structure text
最近用了很多文本配置输入,考虑一个最简单的格式,适合C语言scanf读写数据. 基本数据类型直接使用常用形式. double 直接用小数,对应%f读取. int 直接用十进制整数,对应%d读取. ch ...
- ProtoBuf练习(五)
表类型 protobuf语言的maps字段类型相当于C++语言的std::map类型 工程目录结构 $ ls proto/ sample_maps.proto proto文件 $ cat proto/ ...
- 三种Hash算法对比以及秒传原理.
三种Hash算法对比以及秒传原理 CRC (32/64) MD5 Sha1 分5个点来说 1.校验值长度 2.校验值类别 3.安全级别 4.应用场景 1).校验值长度 CRC(32/64) 分别 ...
- 基于Canvas 实现图片转点阵图
要实现什么 同事想做一张世界地图的背景图,但是网上找的图片都太low了.他想用那种密集的点阵组成的世界地图.作为程序员,想法当然是通过图片处理,识别像素点,然后生成新的图片. 目标是这样的: 实现思路 ...
- 三层登录——C#版
前言 前期了解三层架构主要是由UI层.BLL层和DAL层三部分构成.看到大牛们都采用三层的思想实现了登录,本菜鸟暗暗地站在了他们的肩膀上. 自己理解 对于三层自己的理解是:就像我们对一个大型的公司去找 ...