文章目录

  • 题目原文
    • Input Specification:
    • Output Specification:
    • Sample Input:
    • Sample Output:
  • 生词如下:
  • 题目大意:
  • 思路如下:
  • 测试点二报错的解决方法
  • 我的代码如下:
  • 柳神的分析
  • 柳神的代码:

强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬

本文由参考于柳神博客写成

柳神的CSDN博客,这个可以搜索文章

柳神的个人博客,这个没有广告,但是不能搜索

还有就是非常非常有用的 算法笔记 全名是

算法笔记  上级训练实战指南       //这本都是PTA的题解
算法笔记

PS 今天也要加油鸭

题目原文

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

生词如下:

不用翻译看的有点懵懂-好像是猜出来了意思,但是又不敢确定.

duplication 副本 +de 就是去除重复的

absolute 绝对的

separate 分开的

题目大意:

给你一个链表

然后你删去重复的结点 重复指的是结点的值 的绝对值重复

例如

23854 -15 00000
87654 15 -1

我们要删除 87654 15 -1 的这个结点

-15 先出现,保留, 15后出现,删去.

思路如下:

创建多个表

一个存储我们输入的

一个存储最后排好的结果

一个存储被我们删去的结果

用一个bool 类型的表来判断值出现过没有.没有出现就是false 出现了就是true

测试点二报错的解决方法

测试点二测试的就是没有重复元素怎么办

可以试试这个测试点

00100 2
00100 21 23857
23857 -1 -1

我的代码如下:

#include<iostream>
#include<math.h>
using namespace std;
struct node {int data;int next;
};
struct node Node[100005], result[100005], remain[100005];
int main(void) {bool Time[100005] = { false };for (int i = 0; i < 100005; ++i)   Time[i] = false;int N=0, FirstAddress=0,temp=0,i=0,j=0,t=0;scanf("%d%d", &FirstAddress, &N);for (i = 0; i < N; ++i) {scanf("%d", &temp);scanf("%d%d", &Node[temp].data, &Node[temp].next);}//cout << "\n\n\n";i = 0;while (FirstAddress != -1) {if (Time[(abs(Node[FirstAddress].data))] == false) {result[i].data = Node[FirstAddress].data;result[i].next = FirstAddress;Time[(abs(Node[FirstAddress].data))] = true;FirstAddress = Node[FirstAddress].next;++i;}else {remain[j].next = FirstAddress;remain[j].data = Node[FirstAddress].data;++j;FirstAddress = Node[FirstAddress].next;}}for ( t = 0; t < i-1; t++) {printf("%05d %d %05d\n", result[t].next, result[t].data, result[t + 1].next);}printf("%05d %d -1\n", result[t].next, result[t].data);for (t = 0; t < j - 1; t++) {printf("%05d %d %05d\n", remain[t].next, remain[t].data, remain[t + 1].next);}if(j!=0)    printf("%05d %d -1", remain[t].next, remain[t].data);return 0;
}

老规矩了,下面是柳神的代码时间

柳神的分析

分析:用结构体数组存储这个链表,大小为maxn = 100000,node[i]表示地址为i的结点。在结构体中定义一个num变量,将num变量先初始化为2 * maxn。通过改变num变量的值最后sort排序来改变链表的顺序。
将没有删除的结点的num标记为cnt1,cnt1为当前没有删除的结点的个数;将需要删除的结点的num标记为maxn + cnt2,cnt2表示当前删除了的结点的个数,因为一开始初始化为了2 * maxn,所以我们可以通过对num排序达到:num = 0~maxn为不删除结点,num = maxn~2maxn为删除结点,num = 2maxn为无效结点
这样sort后就会按照需要输出的顺序将结点排序,我们只需要输出前cnt1+cnt2个结点即可~~

柳神的代码:

#include <cstdio>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int maxn = 100000;
struct NODE {int address, key, next, num = 2 * maxn;
}node[maxn];
bool exist[maxn];
int cmp1(NODE a, NODE b){return a.num < b.num;
}
int main() {int begin, n, cnt1 = 0, cnt2 = 0, a;scanf("%d%d", &begin, &n);for(int i = 0; i < n; i++) {scanf("%d", &a);scanf("%d%d", &node[a].key, &node[a].next);node[a].address = a;}for(int i = begin; i != -1; i = node[i].next) {if(exist[abs(node[i].key)] == false) {exist[abs(node[i].key)] = true;node[i].num = cnt1;cnt1++;}else {node[i].num = maxn + cnt2;cnt2++;}}sort(node, node + maxn, cmp1);int cnt = cnt1 + cnt2;for(int i = 0; i < cnt; i++) {if(i != cnt1 - 1 && i != cnt - 1) {printf("%05d %d %05d\n", node[i].address, node[i].key, node[i+1].address);} else {printf("%05d %d -1\n", node[i].address, node[i].key);}}return 0;
}

如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗

相信我,你也能变成光.

如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.

PTA甲级 1097 Deduplication on a Linked List (25分)-链表处理相关推荐

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

    Deduplication意为重复数据消除,Deplicated意为复制,是deduplication的过去式,题干大意为给定一个单链表L要求你对每一个结点的值判断,如果这个值的绝对值是第一次出现,那 ...

  2. 【PAT甲级 链表去重】1097 Deduplication on a Linked List (25 分) C++ 全部AC

    题目 思路: 先把题目中的链表读进内存 创建两个新链表listA, listB 遍历原链表,把重复的放进listB,不重复的放进listA.放的时候,注意处理一下后继next中存的值. 分别打印两个分 ...

  3. 1097 Deduplication on a Linked List (25 分)_35行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a singly linked list L with integer keys, you are supposed ...

  4. PAT甲级1097 Deduplication on a Linked List:[C++题解]遍历链表、两个vector

    文章目录 题目分析 题目链接 题目分析 题意:删掉链表中数值绝对值相同的结点,将其放入另一个链表中.最后输出去重后的链表和删掉元素构成的链表. 分析:采用数组模拟链表,把链表存下来.然后遍历链表,该元 ...

  5. 附加3 Merging Linked Lists (25 分)

    Given two singly linked lists L 1=a 1 →a 2​ →⋯→a n−1​ →a n and L 2 =b 1 →b 2 →⋯→b m−1 →b m . If n≥2m ...

  6. PTA 09-排序3 Insertion or Heap Sort (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/676 5-14 Insertion or Heap Sort   (25分) Accor ...

  7. PTA 01-复杂度2 Maximum Subsequence Sum (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum   (25分) Given ...

  8. 1097 Deduplication on a Linked List

    1. 开始测试点4不通过,检查后发现是犯了低级错误,把表示绝对值有无出现的整型数组的大小设置为了4000(题目中说绝对值不会超过10的4次方),所以最小也该是10001. 2. 我认为和其他链表题相比 ...

  9. 1074 Reversing Linked List (25 分) java 题解

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

最新文章

  1. BZOJ5323 洛谷4562:[JXOI2018]游戏——题解
  2. 作为程序员我给csdn博客添加打赏功能
  3. VTK:网格之WindowedSincPolyDataFilter
  4. 怎么merge分支_实战 Git 分支策略
  5. easyui一行显示多行_easyui datagrid以及oracle中的多行合并一行
  6. 电商企业怎样用好大数据
  7. Java 四种线程池的用法分析
  8. shell统计游戏活跃用户数之改进过程分析
  9. Word 2010/2013 菜单栏添加 MathType 菜单
  10. 190119每日一句
  11. 代码对比工具,就用这7个!
  12. Xshell安装宝塔Linux面板教程
  13. aspcms cookies欺骗和后台无验证注入
  14. buct2018年程序设计实训作业一部分题目解答
  15. 以太坊数字资产的发行和流通:以太坊上的数字资产定义、ERC 20代币合约标准、ERC 20标准接口、ERC 721代币合约标准、
  16. JAVA打字小游戏,面向对象完成
  17. 大学生初涉职场十一大病毒
  18. 用定时器Timer方式实现LED周期性闪烁
  19. 上海市历年平均工资及社保基数
  20. 新手必学的的web渗透测试查找漏洞(一)

热门文章

  1. 舞动的表情包——浅析 GIF 格式图片的存储和压缩
  2. 感悟开发.Net代码自动生成器,为软件起个好名字
  3. 楼天城:世界顶尖黑客、曾被脸书谷歌青睐的中国大学生编程第一人
  4. 机械制造自动化类毕业论文文献有哪些?
  5. 不同粒径大小的金纳米粒子|单分散小粒径金纳米颗粒|黄色纳米颗金粒粒径2nm
  6. 相声《我的大学生活》台词
  7. 天嵌TQ210挂载NFS文件系统详细步骤
  8. Planet-在Word里添加格式美丽的代码
  9. 【QT】QT Qtimer定时的使用 含屏保功能实现,鼠标无操作后处理事件
  10. android 防火墙,安卓防火墙-安卓防火墙(Android Firewall) 安卓版v2.2.2-PC6安卓网