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 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<iostream>
#include<math.h>
#include<vector>
using namespace std;
struct node{
int id,data,next;
};
int main(){
int s,n,b=-1,index,check[10000]={0},t1=-1,last=-1,tag=0;
cin>>s>>n;
node list[100000];
for(int i=0;i<n;i++){
node nod;
cin>>nod.id>>nod.data>>nod.next;
list[nod.id]=nod;
}
index=s;
vector<node> single,del;
while(index!=-1){
if(check[abs(list[index].data)]==0)
single.push_back(list[index]);
else
del.push_back(list[index]);
check[abs(list[index].data)]=1;
index=list[index].next;
}
for(int i=0;i<single.size();i++){
if(i!=single.size()-1)
printf("%05d %d %05d\n",single[i].id,single[i].data,single[i+1].id);
else
printf("%05d %d %d\n",single[i].id,single[i].data,-1);
}
for(int i=0;i<del.size();i++){
if(i!=del.size()-1)
printf("%05d %d %05d\n",del[i].id,del[i].data,del[i+1].id);
else
printf("%05d %d %d\n",del[i].id,del[i].data,-1);
}
return 0;
}
PAT 1097. Deduplication on a Linked List (链表)的更多相关文章
- PAT甲级——1097 Deduplication on a Linked List (链表)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...
- 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 (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甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
- 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 ...
- 【PAT甲级】1097 Deduplication on a Linked List (25 分)
题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...
- PAT (Advanced Level) 1097. Deduplication on a Linked List (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- 1097 Deduplication on a Linked List
题意: 给出一个链表,删除绝对值相同的结点,对于每个绝对值为K的结点,只保留第一次出现的那个.把被移除的结点组成一个新链表,输出删除去重后的链表和新链表. 思路:考察链表的“删除”操作,不难. 代码: ...
随机推荐
- go语言笔记——多值函数,本质上和nodejs的回调很像,不过nodejs是回调的第一个参数是err,而golang里是第二个!
5.2 测试多返回值函数的错误 Go 语言的函数经常使用两个返回值来表示执行是否成功:返回某个值以及 true 表示成功:返回零值(或 nil)和 false 表示失败(第 4.4 节).当不使用 t ...
- [Supervisor]supervisor监管gunicorn启动DjangoWeb时异常退出
一开始配置 [program:django_web] command=gunicorn -w 4 -b 0.0.0.0:8080 superadmin.wsgi:application directo ...
- spark 操作Hive时遇到的问题
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).17/10/14 ...
- 91. Ext中获取combobox中的valueField和displayField的值
转自:https://blog.csdn.net/jcy472578/article/details/42113119Ext.getCmp("schemaVersion").val ...
- 【转载】sql索引存储结构
一.引言 对数据库索引的关注从未淡出我的们的讨论,那么数据库索引是什么样的?聚集索引与非聚集索引有什么不同?希望本文对各位同仁有一定的帮助.有不少存疑的地方,诚心希望各位不吝赐教指正,共同进步.[最近 ...
- 最少拦截系统------LCS--------动态规划
这是一道极好的题,会了这个应该说 最长递增子序列什么的 就有了另外一种思路了 下面附上代码---应该仔细的看一下 那个 if判断 #include<stdio.h> #include ...
- 数据清洗——python定位csv中的特定字符位置
之前发过一篇关于定位csv中的特殊字符的,主要是用到了python的自带的函数,近期又遇到了一些新的问题,比如isdigit()的缺点在于不能判断浮点型,以及小数中有多个小数点的情况.发现还是正则表达 ...
- 【转】Linux GCC常用命令
转自:http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html 1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Comp ...
- [转]linux之cut命令的用法
转自:http://www.jb51.net/article/41872.htm cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对“行”来进行分析的,并不是整 ...
- 关于学习C语言
c语言作为一种计算机的语言,我们学习它,有助于我们更好的了解计算机,与计算机进行交流,因此,c语言的学习对我们尤其重要. 在这个星期里,我们专业的学生在专业老师的带领下进行了c语言程序实践学习.在这之 ...