PAT甲级——1097 Deduplication on a Linked List (链表)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982
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 Lbeing 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 Address is the position of the node, Key is an integer of which absolute value is no more than 1, 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
题目大意:从头结点开始遍历一个链表,将遇到的节点的key的绝对值标记,若重复,则将此节点放到另一个链表里;举个例子,List 21→-15→-15→-7→15,答案是 21→-15→-7 和 -15→15 两个链表。
思路:链表的节点地址是5位整数,-1表示Null ,用数组存放链表。基础的链表删减操作(其实只要更改相应节点的next地址),用 pre 记录前一个节点的地址,addr 记录当前节点的地址,数组也好、set也好、map也好,设置一个容器用于标记已经出现过的abs( key )。
#include <iostream>
#include <unordered_map>
#include <vector>
#include <cmath>
#define MaxNum 100001
using namespace std; struct node {
int key, next = -;
}; vector <node> List(MaxNum);
unordered_map <int, bool> S; int main()
{
int head, N;
scanf("%d%d", &head, &N);
for (int i = ; i < N; i++) {
int addr;
scanf("%d", &addr);
scanf("%d%d", &List[addr].key, &List[addr].next);
}
int pre = head, addr = List[head].next, secHead = -, secAddr;
S[abs(List[head].key)] = true; while (addr != -) {
if (S[abs(List[addr].key)]) {
List[pre].next = List[addr].next;
if (secHead == -) {
secHead = addr;
secAddr = secHead;
List[secHead].next = -;
}
else {
List[secAddr].next = addr;
secAddr = addr;
List[secAddr].next = -;
}
addr = List[pre].next;
}
else {
S[abs(List[addr].key)] = true;
pre = addr;
addr = List[addr].next;
}
}
for (addr = head; addr != -; addr = List[addr].next) {
printf("%05d %d ", addr, List[addr].key);
List[addr].next == - ? printf("-1\n") : printf("%05d\n", List[addr].next);
}
for (secAddr = secHead; secAddr != -; secAddr = List[secAddr].next) {
printf("%05d %d ", secAddr, List[secAddr].key);
List[secAddr].next == - ? printf("-1\n") : printf("%05d\n", List[secAddr].next);
}
return ;
}
PAT甲级——1097 Deduplication on a Linked List (链表)的更多相关文章
- 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 ...
- PAT甲级——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 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[比较]
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甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
- 【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 (25)
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...
随机推荐
- 用VBA计算两个日期之间的工作日(去掉周末两天)
最近公司HR和Finance想算员工的工作天数,想让我帮忙写些VBA,自己从网上找了下代码,自己再改改,以下来自网络. 计算两个日期之间的工作日,用VBA,因量大,最好用数组做 Sub kk() Di ...
- C++STL库中vector容器常用应用
#include<iostream> #include<vector> #include<algorithm> using namespace std; int m ...
- puppet多环境配置(puppet自动化系列2)
三.Puppet多环境部署 我们为puppetmaster建立3个环境,它们分别是开发环境(jqdev).测试环境(jqtest).生产环境(jqprd). 3.1 配置puppet.conf 在标签 ...
- springMVC绑定json参数之二(2.2.2)
二.springmvc 接收不同格式的json字符串 2).格式二:json字符串数组 前台: test = function () { var test = ["123",&qu ...
- 最近一直是web前段,没什么意思,所以就不发资料了
最近一直是web前段,没什么意思,所以就不发资料了 版权声明:本文为博主原创文章,未经博主允许不得转载.
- position应用之相对父元素的定位
分别添加以下style即可: 父元素position:relative; 子元素position:absolute; right:0px; bottom:0px;
- Linux下统计代码行数
使用wc统计代码行数 最近写了一些代码,想统计一下代码的行数,在eclipse中好像没这功能,网上搜了一下才发现原来Linux有一个统计文件行数的命令wc.使用wc可以打印出每个文件和总文件的行数.字 ...
- Struts2工作原理和执行流程图
在struts2的应用中,从用户请求到服务器返回相应响应给用户端的过程中,包含了许多组件如:Controller.ActionProxy.ActionMapping.Configuration Man ...
- eclipse中使用Maven插件报错:-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
步骤: 1.添加M2_HOME的环境变量 2.Preference->Java->Installed JREs->Edit 选择一个jdk 3.添加 -Dmaven.multiMod ...
- 具体问题:3、hibernate跟Mybatis/ ibatis 的区别,为什么选择?
第一章 Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...