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 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
题目分析
已知N个结点,对链表中结点data绝对值相同的结点进行删除(保留绝对值相同第一次出现的结点),分别打印链表中留下的结点,链表中删除的结点
解题思路
思路 01
- 将所有节点保存与数组中,下标即为地址值,节点order属性记录该节点在链表中的序号初始化为2*maxn。bool exist[maxn]存放已经出现过的绝对值
- 从头节点开始依次遍历节点,将已出现过的绝对值标记为true
2.1 若当前节点的data绝对值未出现过,order赋值为cnt1++
2.2 若当前节点的data绝对值已出现过,order赋值为maxn+cnt2++(为了将其排在后面) - 打印0cnt1+cnt2-1的结点(从0cnt1-1为链表中留下的结点,从cnt1~cnt2-1为链表中删除的结点)
思路 02
- 将所有节点保存与数组中,下标即为地址值,节点order属性记录该节点在链表中的序号初始化为2*maxn。bool exist[maxn]存放已经出现过的绝对值
- 从头节点开始依次遍历节点,将已出现过的绝对值标记为true
2.1 若当前节点的data绝对值未出现过,将其存放于vector r中(r保存链表中保留的结点);
2.2 若当前节点的data绝对值已出现过,将其存放于vector d中(d保存链表中删除的结点); - 分别打印r和d中的结点
Code
Code 01(最优)
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const int maxn=100010;
struct node {
int adr;
int data;
int next;
int order=2*maxn;
} nds[maxn];
bool exist[maxn];
bool cmp(node &n1,node &n2) {
return n1.order<n2.order;
}
int main(int argc,char * argv[]) {
int hadr,n,adr;
scanf("%d %d",&hadr,&n);
for(int i=0; i<n; i++) {
scanf("%d",&adr);
scanf("%d %d",&nds[adr].data,&nds[adr].next);
nds[adr].adr=adr;
}
int cnt1=0,cnt2=0;
for(int i=hadr; i!=-1; i=nds[i].next) {
if(exist[abs(nds[i].data)]) {
nds[i].order=maxn+cnt2++;
} else {
exist[abs(nds[i].data)]=true;
nds[i].order=cnt1++;
}
}
sort(nds,nds+maxn,cmp);
int cnt=cnt1+cnt2;
for(int i=0; i<cnt; i++) {
printf("%05d %d",nds[i].adr,nds[i].data);
if(i==cnt1-1||i==cnt-1)printf(" -1\n");
else printf(" %05d\n",nds[i+1].adr);
}
return 0;
}
Code 02
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const int maxn=100010;
struct node {
int adr;
int data;
int next;
int order=maxn;
} nds[maxn];
bool exist[maxn];
bool cmp(node &n1,node &n2) {
return n1.order<n2.order;
}
int main(int argc,char * argv[]) {
int hadr,n,adr;
scanf("%d %d",&hadr,&n);
for(int i=0; i<n; i++) {
scanf("%d",&adr);
scanf("%d %d",&nds[adr].data,&nds[adr].next);
nds[adr].adr=adr;
}
int count=0;
for(int i=hadr; i!=-1; i=nds[i].next) {
nds[i].order=count++;
}
n=count;
sort(nds,nds+maxn,cmp);
vector<node> r;
vector<node> d;
for(int i=0; i<count; i++) {
if(exist[abs(nds[i].data)]) {
d.push_back(nds[i]);
} else {
exist[abs(nds[i].data)]=true;
r.push_back(nds[i]);
}
}
for(int i=0; i<r.size(); i++) {
printf("%05d %d",r[i].adr,r[i].data);
if(i==r.size()-1)printf(" -1\n");
else printf(" %05d\n",r[i+1].adr);
}
for(int i=0; i<d.size(); i++) {
printf("%05d %d",d[i].adr,d[i].data);
if(i==d.size()-1)printf(" -1\n");
else printf(" %05d\n",d[i+1].adr);
}
return 0;
}
PAT Advanced 1097 Deduplication on a Linked List (25) [链表]的更多相关文章
- PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. ...
- PAT甲级——1097 Deduplication on a Linked List (链表)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...
- 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 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 分)
题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...
- 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 ...
- pat1097. Deduplication on a Linked List (25)
1097. Deduplication on a Linked List (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ...
- 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 1097. Deduplication on a Linked List (链表)
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...
随机推荐
- swift中数据之间的转换
1.swift 开发 - NSDictionary与NSData互转.Dictionary与Data互转 https://blog.csdn.net/SuperMageHuang/article/de ...
- 前端第二篇---前端基础之CSS
前端第二篇---前端基础之CSS 目录 一.css介绍 二.css语法 三.css的几种引入方式 四.css选择器 五.css属性相关 六.盒子模型 拓展 一.css介绍 CSS(Cascading ...
- kubernter相关内容
1. Kubernetes 第一章:互联网架构的演变 随着1946年世界上第一台电子计算机的问世网络就随之出现了,只不过当初只是为了解决多个终端之间的连接,这就是局域网的雏形.后来,随着美国国防部高级 ...
- 【LeetCode】 240. 搜索二维矩阵 II
题目 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 mat ...
- UVA - 1153 Keep the Customer Satisfied(顾客是上帝)(贪心)
题意:有n(n<=800000)个工作,已知每个工作需要的时间qi和截止时间di(必须在此之前完成),最多能完成多少个工作?工作只能串行完成.第一项任务开始的时间不早于时刻0. 分析:按截止时间 ...
- 【pwnable.kr】leg
pwnable从入门到放弃第八题. Download : http://pwnable.kr/bin/leg.cDownload : http://pwnable.kr/bin/leg.asm ssh ...
- tableau创建定量值地图
一.官方案例:定量值地图的创建 数据形式: 过程: 分别双击经纬度(默认的纬度会自动添加到行功能区,经度到列功能区) 将Id放入标记卡详细信息 将Magnitude^10(震级^10)放入大小,(该 ...
- 【LeetCode】最长公共子序列
[问题]给定两个字符串A和B,长度分别为m和n,要求找出它们最长的公共子串,并返回其长度.例如:A = "HelloWorld"B = "loop"则A与B的最 ...
- hdu 3388 Coprime
第一个容斥的题,感觉这东西好神啊.于是扒了一发题解2333 首先想对于[1,x]内有多少与n,m都互质的数,显然x是存在单调性的,所以可以二分一下. 那么互质的数的求法,就是x-存在n,m一个质因数的 ...
- Python Learning Day7
破解极验滑动验证 博客园登录url: https://account.cnblogs.com/signin?returnUrl=https%3A%2F%2Fwww.cnblogs.com%2F 代码逻 ...