02-线性结构3 Reversing Linked List
02-线性结构3 Reversing Linked List (25分)
- 时间限制:400ms
- 内存限制:64MB
- 代码长度限制:16kB
- 判题程序:系统默认
- 作者:陈越
- 单位:浙江大学
https://pta.patest.cn/pta/test/3512/exam/4/question/62614
Given a constant KKK and a singly linked list LLL, you are supposed to reverse the links of every KKK elements on LLL. For example, given LLL being 1→2→3→4→5→6, if K=3K = 3K=3, then you must output 3→2→1→6→5→4; if K=4K = 4K=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 NNN (≤105\le 10^5≤105) which is the total number of nodes, and a positive KKK (≤N\le N≤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 NNN 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
#include<bits/stdc++.h>
using namespace std; struct Node
{
int data;
int adr,next;
}t;
int N,head,K;
map<int,Node> Input;//用map储存输入序列,地址作为键值,方便通过地址(head,next)找到相关点
stack<Node> Sta,T;
vector<Node> Result(N);//储存结果链表
vector<Node>::iterator it;
int main()
{
/*输入*/
scanf("%d %d %d",&head,&N,&K);
for(int i=;i<N;i++)
{
scanf("%d %d %d",&t.adr,&t.data,&t.next);
Input[t.adr]=t;
}
/*处理*/
int num=;
t=Input.find(head)->second;//找到第一个点
while(num++<N)//对n个元素入栈处理
{
int flag=t.next;
Sta.push(t);
if(t.next!=-)
t=Input.find(t.next)->second;
if(num%K==)//满K个元素,逆序出栈放入Result
{
while(!Sta.empty())
{
Result.push_back(Sta.top());
Sta.pop();
}
if(flag==-)//最后一个结点刚好组成最后K个,结束
break;
continue;
}
else if(flag==-)//最后一个结点多余,将栈中的点出,入,出栈后恢复正序放入Result,结束
{
while(!Sta.empty())
{
T.push(Sta.top());
Sta.pop();
}
while(!T.empty())
{
Result.push_back(T.top());
T.pop();
}
break;
}
}
/*输出*/
it=Result.begin();
printf("%05d %d ",it->adr,it->data);
it++;
for(;it!=Result.end();it++)
{
printf("%05d\n%05d %d ",it->adr,it->adr,it->data);
}
printf("-1\n");
Input.clear();
Result.clear();
return ;
}
02-线性结构3 Reversing Linked List的更多相关文章
- pat02-线性结构1. Reversing Linked List (25)
02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...
- 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】
02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...
- 02-线性结构2 Reversing Linked List
由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are ...
- PTA 02-线性结构3 Reversing Linked List (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List (25分) Given a ...
- 数据结构练习 02-线性结构2. Reversing Linked List (25)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 02-线性结构3 Reversing Linked List (25 分)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 02-线性结构3 Reversing Linked List (25 分)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 02-线性结构3 Reversing Linked List
题目 Sample Input: 00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 ...
- PAT02-线性结构3 Reversing Linked List
题目:https://pintia.cn/problem-sets/1010070491934568448/problems/1037889290772254722 先是看了牛客(https://ww ...
随机推荐
- app测试的一些较为重要的测试点
安装测试 从不同的手机所自带的不同的版本的软件商城里面下载抖音并安装查看是否成功 安装后是否能正常运行安装后的文件和文件夹是否写到了指定的目录里 安装过程中取消安装,安装的文件是否在指定的目录里 安装 ...
- Property [*****] not found on type [com.erp.pojo.ErpSupplier]
我实体类里用的是 springboot 里@Slf4j @Data 注解式写的 这样可以减少代码量 ,但是遇到一个问题影响我好长时间 出现了这个错误 Property [*****] not ...
- css定位有哪几种方式
一.position 属性规定元素的定位类型,它一般有以下四个值: 默认static 相对定位relative 绝对定位absolute 固定定位fixed 元素可以使用的顶部,底部,左侧和右侧属性定 ...
- [算法总结]DFS(深度优先搜索)
目录 一.关于DFS 1. 什么是DFS 2. DFS的搜索方式 二.DFS的具体实现 三.剪枝 1. 顺序性剪枝 2. 重复性剪枝 3. 可行性剪枝 4. 最优性剪枝 5. 记忆化剪枝 四.练习 一 ...
- Progress笔记
1. iconv -f gbk -t unicode test.tmp > test.csv 如果出现文件数据补全,需要确认在这之前,output是否已经close,如果output to指定了 ...
- JuiceSSH:安卓平台免费好用的 SSH 客户端
为了解决上下班路上或者没带电脑时,查看 Linux 服务器日志或者紧急运维的需求,最终找到了 JuiceSSH 这款软件,强烈推荐给大家. 简介 JuiceSSH 是一个为 Android 打造的全功 ...
- 你知道如何自动保存 Spring Boot 应用进程号吗
1. 前言 欢迎阅读 Spring Boot 2 实战 系列文章. PID 对于系统运维来说并不陌生,但是对于一些开发者特别是新手还是要简单介绍一下的.它是 Process ID 的简称,是系统分配给 ...
- 怎么在执行Python脚本时,密码等敏感信息也不让它出现
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取http ...
- 基于canvas的画板
最近重新在看Html5&CSS3的知识,看到canvas的时候,想到了以前在学校学计算机图形学时做过的画图实验,于是想,可以基于html5和css3来做一款画板,经过1天的努力,完成了画板的一 ...
- 性能测试-pidstat 问题定位分析
pidstat 概述 pidstat是sysstat工具的一个命令,用于监控全部或指定进程的cpu.内存.线程.设备IO等系统资源的占用情况.pidstat首次运行时显示自系统启动开始的各项统计信息, ...