1032 Sharing (25分)

回顾了下链表的基本使用,这题就是判断两个链表是否有交叉点。

我最开始的做法就是用cnt[]记录每个节点的入度,发现入度为2的节点即为答案。后来发现这里忽略了两个链表初始节点都是一样的情况,于是这里cnt[]的含义变为每个节点被引用的次数,当一个节点被引用两次就说明被两个链表同时引用。此时又通过了部分测试样例,可是测试样例5始终通过不了。后来偶然翻到一篇博客才恍然大悟,这里的节点可能不止包含两个单词,即可能有多个起点表示多个单词,而题目只是问你给定的两个单词有没有相同后缀,那之前的思路就不适用了。

正确的做法应当是先遍历第一个单词,给所有节点的引用次数+1;接着遍历第二个单词,同样给路径上的节点引用次数+1,发现有两次引用的节点即为答案。最后记得要控制输出的格式,这点在很多题目中都考察过

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <set>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 1e5+100;
struct node{
char data;
int nxt = -1;
}link[maxn];
int pa, pb, m, now, val, nextNode;
int cnt[maxn], res = -1;
int main(){
scanf("%d%d%d", &pa, &pb, &m);
while(m--){
scanf("%d %c %d", &now, &val, &nextNode);
link[now].data = val, link[now].nxt = nextNode;
}
while(pa!=-1) cnt[pa]++, pa = link[pa].nxt;
while(pb!=-1){
if(++cnt[pb]==2){
res = pb;
break;
}
pb = link[pb].nxt;
}
if(res==-1) printf("-1");
else printf("%05d", res);
}

Reference:

https://www.amoshuang.com/archives/774

https://blog.csdn.net/qq_39072627/article/details/107008104

1052 Linked List Sorting (25分)

题意对链表进行排序并输出。有了1032这题的经验,WA后很快就发现输入的数据不一定在都在链表中。修改过来后缝缝补补,很快就AC了,不过需要注意一些细节,不然也可能过不了

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <set>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 1e5+100;
pii g[maxn];
int n, pos, now, val, nextNode;
struct node{
int val, nxt;
}link[maxn];
int main(){
scanf("%d%d", &n, &pos);
for(int i = 1; i <= n; i++){
scanf("%d%d%d", &now, &val, &nextNode);
link[now] = {val, nextNode};
}
int t = 0;
while(pos!=-1) g[++t] = {link[pos].val, pos}, pos =link[pos].nxt;
sort(g+1, g+1+t);
printf("%d ", t);
if(t==0) printf("-1\n");
else{
printf("%05d\n", g[1].second);
for(int i = 1; i <= t; i++){
printf("%05d %d ", g[i].second, g[i].first);
if(i!=t) printf("%05d\n", g[i+1].second);
else printf("-1\n");
}
} }

Reference:

https://blog.csdn.net/qq_39072627/article/details/107009532

https://blog.csdn.net/LSC_333/article/details/91356270

1074 Reversing Linked List (25分)

按照给定的k值,链表每k个节点为一组翻转一次,我的思想是对的,但是实现的方法有点啰嗦不直观,导致WA后调试起来也很麻烦。后面参考别人的做法,记录整个链表翻转后的下标,直接输出即可

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <set>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 1e5+100;
pii g[maxn];
int n, pos, k, now, val, nextNode;
int res[maxn];
struct node{
int val, nxt;
}link[maxn];
int main(){
scanf("%d%d%d", &pos, &n, &k);
for(int i = 1; i <= n; i++){
scanf("%d%d%d", &now, &val, &nextNode);
link[now] = {val, nextNode};
}
int t = 0;
while(pos!=-1) g[t++] = {link[pos].val, pos}, pos =link[pos].nxt;
for(int i = 0; i <= t-1; i++) {
if(i<t-t%k) res[i] = (i/k)*k*2+k-1-i;
else res[i] = i;
}
for(int i = 0; i <= t-1; i++){
printf("%05d %d ", g[res[i]].second, g[res[i]].first);
if(i!=t-1) printf("%05d\n", g[res[i+1]].second);
else printf("-1\n");
}
}

Reference:

https://blog.csdn.net/qq_41325698/article/details/103466109

1097 Deduplication on a Linked List (25分)

题目要求去除链表上权值的绝对值相同的节点(只保留第一个),之后把未删除的节点按链表连接顺序输出,接着把被删除节点也按在原链中的顺序输出。

这题的意思我大概看懂了,强迫自己没有看翻译,因为正式考试的时候是不让翻译的,想着没过的话再看翻译,没想到一遍就过了,看懂题目意思后,用链表去模拟过程即可。然后感觉代码可以再写的简洁一点,原节点和处理后答案的存储可以都是用结构体或者是pair

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <set>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 1e5+100;
pii g[maxn], h[maxn];
int n, pos, now, val, nextNode;
bool vis[maxn];
struct node{
int val, nxt;
}link[maxn];
int main(){
scanf("%d%d", &pos, &n);
for(int i = 1; i <= n; i++){
scanf("%d%d%d", &now, &val, &nextNode);
link[now] = {val, nextNode};
}
int t1 = 0, t2 = 0;
while(pos!=-1) {
int data = link[pos].val;
// cout << data << " " << pos << endl;
if(!vis[abs(data)]) g[t1++] = {data, pos}, vis[abs(data)] = 1;
else h[t2++] = {data, pos};
pos =link[pos].nxt;
}
for(int i = 0; i < t1; i++) {
printf("%05d %d", g[i].second, g[i].first);
if(i!=t1-1) printf("%05d\n", g[i+1].second);
else printf("-1\n");
}
for(int i = 0; i < t2; i++) {
printf("%05d %d", h[i].second, h[i].first);
if(i!=t2-1) printf("%05d\n", h[i+1].second);
else printf("-1\n");
} }

PTA甲级—链表的更多相关文章

  1. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  2. PTA L2-002 链表去重

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805072641245184 第一次做链表题,有时间多看看 解释 ...

  3. PTA L2-002 链表去重 团体程序设计天梯赛-练习集

    L2-002 链表去重(25 分)   给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉.即对每个键值 K,只有第一个绝对值等于 K 的结点被保留.同时,所有被删除的结点须被保存在另 ...

  4. PAT甲级 链表题_C++题解

    链表处理 PAT (Advanced Level) Practice 链表题 目录 <算法笔记> 重点摘要:静态链表 1032 Sharing (25) 1052 Linked List ...

  5. PTA甲级B1061 Dating

    目录 B1061 Dating (20分) 题目原文 Input Specification: Output Specification: Sample Input: Sample Output: 生 ...

  6. PTA 单链表分段逆转

    6-9 单链表分段逆转 (25 分)   给定一个带头结点的单链表和一个整数K,要求你将链表中的每K个结点做一次逆转.例如给定单链表 1→2→3→4→5→6 和 K=3,你需要将链表改造成 3→2→1 ...

  7. PTA 求链表的倒数第m个元素

    6-7 求链表的倒数第m个元素 (20 分)   请设计时间和空间上都尽可能高效的算法,在不改变链表的前提下,求链式存储的线性表的倒数第m(>)个元素. 函数接口定义: ElementType ...

  8. pta L2-002 链表去重 +散列表知识小普及+二进制取反补码运算

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805072641245184: 废话:今天忙着学习新知识了,没怎 ...

  9. PTA 甲级 1139

    https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 其实这道题目不难,但是有很多坑点! 首先数据 ...

随机推荐

  1. js 必须为字母或下划线, 一旦创建不能修改

    <div class="form-group"> <label class="col-lg-2 control-label" for=&quo ...

  2. PHP 爬取图片 保存本地

    public function getImage($url,$filename='') { if($url == ''){ return false; } if($filename == ''){ $ ...

  3. 静默(命令行)安装oracle 11g

    CentOS 6 静默安装oracle 11g 我参考的这个,他非常详细:https://blog.csdn.net/JIANG123456T/article/details/77745892 我只是 ...

  4. 如何在 Vite 中使用 Element UI + Vue 3

    在上篇文章<2021新年 Vue3.0 + Element UI 尝鲜小记>里,我们尝试使用了 Vue CLI 创建 Vue 3 + Element UI 的项目,而 Vue CLI 实际 ...

  5. 9. 细节见真章,Formatter注册中心的设计很讨巧

    目录 本文提纲 版本约定 你好,我是A哥(YourBatman). Spring设计了org.springframework.format.Formatter格式化器接口抽象,对格式化器进行了大一统, ...

  6. Linux 入门教程:00 Background

    Linux 为何物? 就是一个操作系统. Linux 历史: 操作系统始于二十世纪五十年代,当时的操作系统能运行批处理程序.批处理程序不需要用户的交互,它从文件或者穿孔卡片读取数据,然后输出到另外一个 ...

  7. Docker学习笔记之创建Ubuntu基础镜像

    在创建基础镜像之前需要安装Bootstrap工具debootstrap,所以执行命令: sudo apt install debootstrap 软件安装完成后就可以使用debootstrap工具下载 ...

  8. 【Oracle】查看表空间是否为自动扩展

    查看指定的表空间是否为自动扩展 SQL>   select file_name,autoextensible,increment_by from dba_data_files where tab ...

  9. 视频画面中实现人脸遮挡教程 - 基于 TensorFlow 实现

    在进行视频通话时,我们往往需要对画面进行一些实时分析,例如识别画面里的人.车.动物等等.这节里我们将使用时信魔方的人脸监视模块实现人脸被手部遮挡的检测,如下图所示效果: 预备知识 时信魔方的客户端使用 ...

  10. 使用modify修改内表

    modify修改内表,有这样一种方式,MODIFY TABLE itab FROM wa [TRANSPORTING ..]. 然后这里的内表itab是有条件的,这个itab必须要有table key ...