PTA甲级—链表
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甲级—链表的更多相关文章
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- PTA L2-002 链表去重
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805072641245184 第一次做链表题,有时间多看看 解释 ...
- PTA L2-002 链表去重 团体程序设计天梯赛-练习集
L2-002 链表去重(25 分) 给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉.即对每个键值 K,只有第一个绝对值等于 K 的结点被保留.同时,所有被删除的结点须被保存在另 ...
- PAT甲级 链表题_C++题解
链表处理 PAT (Advanced Level) Practice 链表题 目录 <算法笔记> 重点摘要:静态链表 1032 Sharing (25) 1052 Linked List ...
- PTA甲级B1061 Dating
目录 B1061 Dating (20分) 题目原文 Input Specification: Output Specification: Sample Input: Sample Output: 生 ...
- PTA 单链表分段逆转
6-9 单链表分段逆转 (25 分) 给定一个带头结点的单链表和一个整数K,要求你将链表中的每K个结点做一次逆转.例如给定单链表 1→2→3→4→5→6 和 K=3,你需要将链表改造成 3→2→1 ...
- PTA 求链表的倒数第m个元素
6-7 求链表的倒数第m个元素 (20 分) 请设计时间和空间上都尽可能高效的算法,在不改变链表的前提下,求链式存储的线性表的倒数第m(>)个元素. 函数接口定义: ElementType ...
- pta L2-002 链表去重 +散列表知识小普及+二进制取反补码运算
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805072641245184: 废话:今天忙着学习新知识了,没怎 ...
- PTA 甲级 1139
https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 其实这道题目不难,但是有很多坑点! 首先数据 ...
随机推荐
- java对象
原文链接http://zhhll.icu/2020/04/26/java%E5%9F%BA%E7%A1%80/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/java%E5% ...
- 利用PHP递归 获取所有的上级栏目
/** * 获取所有的上级栏目 * @param $category_id * @param array $array * @return array * @author 宁佳兵 <meilij ...
- 【Spring】Spring JdbcTemplate
Spring JdbcTemplate 文章源码 JdbcTemplate 概述 它是 Spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装.Spring 框架提供了很多的操 ...
- MySQL 使用sql添加和创建用户
用户管理 SQL 命令操作 用户表:mysql.user 本质:对mysql.user 表进行增删改查 -- ============== 用户管理 ============= -- 创建用户 -- ...
- 18.java设计模式之中介者模式
基本需求 智能家庭包括各种设备,闹钟.咖啡机.电视机.窗帘等 要看电视时,各个设备可以协同工作,自动完成看电视的准备工作,比如流程为:闹铃响起->咖啡机开始做咖啡->窗帘自动落下-> ...
- 【Spring】Spring中的Bean - 5、Bean的装配方式(XML、注解(Annotation)、自动装配)
Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...
- 【易筋经】Llinux服务器初始化及常用命令大全
Llinux服务器初始化及常用命令大全 1.关闭防火墙以及内核安全机制 systemctl stop firewalld systemctl disable firewalld ##永久性关闭 set ...
- C#使用struct直接转换下位机数据
编写上位机与下位机通信的时候,涉及到协议的转换,比较多会使用到二进制.传统的方法,是将数据整体获取到byte数组中,然后逐字节对数据进行解析.这样操作工作量比较大,对于较长数据段更容易计算位置出错. ...
- Sentry(v20.12.1) K8S 云原生架构探索,1分钟上手 JavaScript 性能监控
系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...
- JS实现计算器,带三角函数,根号
极简主义网页计算器. 实现了按键特效,可响应键盘按键,实时显示计算结果. 可切换模式,拓展高级功能,包括根号.三角函数.括号等. 效果如下: 代码如下: html: <!DOCTYPE html ...