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 ...
随机推荐
- Random获取不重复随机数
Random R = new Random(Guid.NewGuid().GetHashCode()); int i = R.Next(9999);
- Python模块-configparse模块
configparse模块用来解析配置文件 配置文件 [DEFAULT] port = 3306 socket = /tmp/mysql.sock [mysqldump] max_allowed_pa ...
- BarTender SDK 实现调用模板条码打印
Demo:MyZebraPrint 基于BatTender .Net SDK 实现调用模板进行条码打印 有需要的朋友可以拿去研究下 在已经安装了BatTender10.1的电脑里测试通过. 下载地址: ...
- elasticsearch2.x插件之一:kibana
介绍: 要说kibana,就不得不先说一下logstash.这里呢,先要讲个故事.故事是开头是这样的,Logstash早期曾经自带了一个特别简单的logstash-web用来查看ES中的数据,其功能太 ...
- Some of your uncommitted changes would be overwritten by syncing.Please commit your changes then try
解决方法有三种,在GitHub shel中输入以下命令,任选一种方法就能解决问题 git reset --hard HEAD -- Destructive. When you do this you' ...
- 2、Tophat align_summary.txt and samtools flagstat accepted_hits.bam disagree
###https://www.biostars.org/p/195758/ Left reads: Input : 49801387 Mapped : 46258301 (92.9% of input ...
- Linux awk指令详解
简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...
- UVaLive 5031 Graph and Queries (Treap)
题意:初始时给出一个图,每个点有一个权值,三种操作:(1)删除某个边:(2)修改每个点的权值:(3)询问与节点x在一个连通分量中所有点的第K大的权值. 析:首先是要先离线,然后再倒着做,第一个操作就成 ...
- ubuntu下php7安装及配置
直接用apt-get 失败 在官网下安装包http://tw2.php.net/get/php-7.0.18.tar.bz2/from/a/mirror 进行make时 出现错误: libtool: ...
- js捕获activex事件
最近参与了一个项目,我的同事在开发一个ActiveX对象,我帮他编写JS脚本来调用这个对象,其中碰到蛮多问题,最难的就是如何响应由ActiveX对象返回的对象事件.正好,现在一起总结一下. 首先,我来 ...