PAT 1074 Reversing Linked List [静态链表] [无效结点]

2024-04-09 11:32

本文主要是介绍PAT 1074 Reversing Linked List [静态链表] [无效结点],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤10​5​​) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. 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 Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

---------------------------------------这是题目和解题的分割线---------------------------------------

和这道题的思路差不多吧PAT 1097 Deduplication on a Linked List,但是我脑子大概是进浆糊了,最后两个测试点卡了好久,最后发现一个是没看清题目,是反转链表,即按照id倒序输出而不是按照data倒序= = 还有一个是反转的次数是有效结点/K,而不是N/K(明明注意到这一点但傻了忘了这个地方也要改 = = )链表(静态)的题都要记得小心无效结点

大概思路是先按照单链表的顺序排序,再按照k分组排序。

#include<cstdio>
#include<algorithm>const int maxN = 100010;using namespace std;struct node
{int data;int address; //当前地址 int next;int id; //链表次序 
}list[maxN];bool cmp1(node a,node b)
{return a.id<b.id;
}bool cmp2(node a,node b)
{return a.id>b.id;
}int main()
{int begin,n,k,i,j;scanf("%d%d%d",&begin,&n,&k);int address;for(i=0;i<n;i++){scanf("%d",&address);scanf("%d%d",&list[address].data,&list[address].next);list[address].address = address;}//初始化成超大的数,以便排序 for(i=0;i<maxN;i++)list[i].id = 2*maxN;int p = begin,order = 0;//遍历,并标上序号 while(p!=-1){list[p].id = order++;p = list[p].next;}//按单链表的顺序排序 sort(list,list+maxN,cmp1);//是order/k不是n/k 存在无效结点干扰的情况 int turn = order/k;for(i=0,j=0;j<turn;i=i+k,j++)sort(list+i,list+i+k,cmp2);for(i=0;i<order-1;i++)printf("%05d %d %05d\n",list[i].address,list[i].data,list[i+1].address);printf("%05d %d -1\n",list[order-1].address,list[order-1].data);return 0;
}

换了个方法试试,如下:

#include<cstdio>
#include<algorithm>using namespace std;struct node
{int address;int data;int next;int id;
}list[100010],print[100010];bool cmp(node a,node b)
{return a.id>b.id;
}int main()
{int begin,n,k,i,cnt = 0;scanf("%d%d%d",&begin,&n,&k);for(i=0;i<n;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);list[a].address = a;list[a].data = b;list[a].next = c;}//将正确顺序存在另一个结构体中 while(begin!=-1){print[cnt++] = list[begin];print[cnt-1].id = cnt-1;begin = list[begin].next;}int t = 0;//需要reverse时也可以通过设置id,再排序来实现 //注意排序和输出都是cnt不是n,因为题目存在无效结点 for(i=0;i<cnt/k;i++){sort(print+t*k,print+t*k+k,cmp);t++;}for(i=0;i<cnt;i++){printf("%05d %d",print[i].address,print[i].data);if(i!=cnt-1) printf(" %05d\n",print[i+1].address);else printf(" -1\n");}return 0;
}

 

这篇关于PAT 1074 Reversing Linked List [静态链表] [无效结点]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/888040

相关文章

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

Java List 使用举例(从入门到精通)

《JavaList使用举例(从入门到精通)》本文系统讲解JavaList,涵盖基础概念、核心特性、常用实现(如ArrayList、LinkedList)及性能对比,介绍创建、操作、遍历方法,结合实... 目录一、List 基础概念1.1 什么是 List?1.2 List 的核心特性1.3 List 家族成

Java集合中的链表与结构详解

《Java集合中的链表与结构详解》链表是一种物理存储结构上非连续的存储结构,数据元素的逻辑顺序的通过链表中的引用链接次序实现,文章对比ArrayList与LinkedList的结构差异,详细讲解了链表... 目录一、链表概念与结构二、当向单链表的实现2.1 准备工作2.2 初始化链表2.3 打印数据、链表长

通过配置nginx访问服务器静态资源的过程

《通过配置nginx访问服务器静态资源的过程》文章介绍了图片存储路径设置、Nginx服务器配置及通过http://192.168.206.170:8007/a.png访问图片的方法,涵盖图片管理与服务... 目录1.图片存储路径2.nginx配置3.访问图片方式总结1.图片存储路径2.nginx配置

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

C++链表的虚拟头节点实现细节及注意事项

《C++链表的虚拟头节点实现细节及注意事项》虚拟头节点是链表操作中极为实用的设计技巧,它通过在链表真实头部前添加一个特殊节点,有效简化边界条件处理,:本文主要介绍C++链表的虚拟头节点实现细节及注... 目录C++链表虚拟头节点(Dummy Head)一、虚拟头节点的本质与核心作用1. 定义2. 核心价值二

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。

关于跨域无效的问题及解决(java后端方案)

《关于跨域无效的问题及解决(java后端方案)》:本文主要介绍关于跨域无效的问题及解决(java后端方案),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录通用后端跨域方法1、@CrossOrigin 注解2、springboot2.0 实现WebMvcConfig