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

相关文章

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

macOS无效Launchpad图标轻松删除的4 种实用方法

《macOS无效Launchpad图标轻松删除的4种实用方法》mac中不在appstore上下载的应用经常在删除后它的图标还残留在launchpad中,并且长按图标也不会出现删除符号,下面解决这个问... 在 MACOS 上,Launchpad(也就是「启动台」)是一个便捷的 App 启动工具。但有时候,应

java streamfilter list 过滤的实现

《javastreamfilterlist过滤的实现》JavaStreamAPI中的filter方法是过滤List集合中元素的一个强大工具,可以轻松地根据自定义条件筛选出符合要求的元素,本文就来... 目录1. 创建一个示例List2. 使用Stream的filter方法进行过滤3. 自定义过滤条件1. 定

Linux系统中配置静态IP地址的详细步骤

《Linux系统中配置静态IP地址的详细步骤》本文详细介绍了在Linux系统中配置静态IP地址的五个步骤,包括打开终端、编辑网络配置文件、配置IP地址、保存并重启网络服务,这对于系统管理员和新手都极具... 目录步骤一:打开终端步骤二:编辑网络配置文件步骤三:配置静态IP地址步骤四:保存并关闭文件步骤五:重

mybatis-plus分页无效问题解决

《mybatis-plus分页无效问题解决》本文主要介绍了mybatis-plus分页无效问题解决,原因是配置分页插件的版本问题,旧版本和新版本的MyBatis-Plus需要不同的分页配置,感兴趣的可... 昨天在做一www.chinasem.cn个新项目使用myBATis-plus分页一直失败,后来经过多方

MyBatis-Plus中静态工具Db的多种用法及实例分析

《MyBatis-Plus中静态工具Db的多种用法及实例分析》本文将详细讲解MyBatis-Plus中静态工具Db的各种用法,并结合具体案例进行演示和说明,具有很好的参考价值,希望对大家有所帮助,如有... 目录MyBATis-Plus中静态工具Db的多种用法及实例案例背景使用静态工具Db进行数据库操作插入

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

python中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

java两个List的交集,并集方式

《java两个List的交集,并集方式》文章主要介绍了Java中两个List的交集和并集的处理方法,推荐使用Apache的CollectionUtils工具类,因为它简单且不会改变原有集合,同时,文章... 目录Java两个List的交集,并集方法一方法二方法三总结java两个List的交集,并集方法一

Apache伪静态(Rewrite).htaccess文件详解与配置技巧

《Apache伪静态(Rewrite).htaccess文件详解与配置技巧》Apache伪静态(Rewrite).htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令,主要的... 一、.htAccess的基本作用.htaccess是一个纯文本文件,它里面存放着Apache服务器