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 (≤10​5​​) 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 10​4​​, 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 <stdio.h>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
using namespace std;
const int maxn = ; struct node{
int address;
int data;
int next;
}nodes[maxn];
vector<node> res1;
vector<node> res2;
int vis[maxn]={};
int main(){
int st,n;
scanf("%d %d",&st,&n);
for(int i=;i<n;i++){
int s,e,d;
scanf("%d %d %d",&s,&d,&e);
nodes[s].address = s;
nodes[s].data = d;
nodes[s].next = e;
}
int root=st;
while(root!=-){
if(vis[abs(nodes[root].data)]!=){
res1.push_back(nodes[root]);
vis[abs(nodes[root].data)]=;
}
else{
res2.push_back(nodes[root]);
}
root=nodes[root].next;
}
if(!res1.empty()){
printf("%05d %d ",res1[].address,res1[].data);
for(int i=;i<res1.size();i++){
printf("%05d\n%05d %d ",res1[i].address,res1[i].address,res1[i].data);
}
printf("-1\n");
}
if(!res2.empty()){
printf("%05d %d ",res2[].address,res2[].data);
for(int i=;i<res2.size();i++){
printf("%05d\n%05d %d ",res2[i].address,res2[i].address,res2[i].data);
}
printf("-1\n");
}
}

注意点:普通链表题,把结果存储在两个vector里,再输出就ac了

PAT A1097 Deduplication on a Linked List (25 分)——链表的更多相关文章

  1. PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)

    1074 Reversing Linked List (25 分)   Given a constant K and a singly linked list L, you are supposed ...

  2. 【PAT甲级】1097 Deduplication on a Linked List (25 分)

    题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...

  3. 【PAT甲级】1074 Reversing Linked List (25 分)

    题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...

  4. pat1097. Deduplication on a Linked List (25)

    1097. Deduplication on a Linked List (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ...

  5. PTA 02-线性结构3 Reversing Linked List (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List   (25分) Given a ...

  6. PAT甲级:1036 Boys vs Girls (25分)

    PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...

  7. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  8. PAT-2019年冬季考试-甲级 7-2 Block Reversing (25分) (链表转置)

    7-2 Block Reversing (25分)   Given a singly linked list L. Let us consider every K nodes as a block ( ...

  9. 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 ...

随机推荐

  1. mongodb 权限设置--用户名、密码、端口

    转自:http://www.cnblogs.com/valor-xh/p/6369432.html 一.关于权限的默认配置 在默认情况下,mongod是监听在0.0.0.0之上的,任何客户端都可以直接 ...

  2. 【RabbitMQ】2、心得总结,资料汇总

    Spring AMQP中文文档 http://ju.outofmemory.cn/entry/320538 云栖社区    https://yq.aliyun.com/search?q=rabbitm ...

  3. canvas-star2.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. canvas-2lineCap.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 【20190123】JavaScript-轮播图特效中出现的问题

    使用纯html和JavaScript实现焦点轮播图特效,本来之前用setInterval()函数写的一个简单的循环轮播图,但是出现了两个问题:   1. 当网页被切换时,也就是网页失去焦点时,计时器函 ...

  6. GitHub使用SSHkey进行连接

    SSH key的配置基本是我们使用git必备的配置,配置好可以避免频繁的在git push或者git pull的时候输入账号和密码 本来我的SSH key早就配置好了,结果他不起作用了,那就在配置一次 ...

  7. React 入门学习笔记整理(三)—— 组件

    1.定义组件 1)函数组件 function GreateH(props){ return <div> <h2>hello,{props.name}</h2> &l ...

  8. [VUE ERROR] Duplicate keys detected: 'tab-user'. This may cause an update error.

    错误消息如图: 如果你看到此错误消息,则说明 v-for 指令的 key值 重复了,只需修改你的 key值 让其不会重复即可.

  9. SD从零开始71 业务信息仓库(BW)

    SD从零开始71 业务信息仓库(BW)概念 在线事务处理的环境OLTP Environment 在事务处理中,我们不断地填充用于跟踪我们的业务流程的数千个不同步骤的特定的表: 例如,销售凭证行条目来自 ...

  10. ArcGIS地图文档优化 mxdPerfstat工具使用体验

    经常有客户会咨询到如何提高地图的显示性能.为何ArcMap刷新地图那么缓慢.为何地图服务响应要等待10多秒? 诸如这些问题,虽然它们的表象都是相似的,但是往往在分析排查问题的时候,我们发现背后的原因是 ...