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 ...
随机推荐
- 页面静态化--Thymeleaf
1.Thymeleaf简介 官方网站:https://www.thymeleaf.org/index.html Thymeleaf是用来开发Web和独立环境项目的现代服务器端Java模板引擎. Thy ...
- php zip打包
zip中加入文件 <?php $zip = new ZipArchive; if ($zip->open('test.zip', ZipArchive::OVERWRITE) === TR ...
- ThinkPHP3.1.2 使用cli命令行模式运行
ThinkPHP3.1.2 使用cli命令行模式运行 标签(空格分隔): php 前言 thinkphp3.1.2 需要使用cli方法运行脚本 折腾了一天才搞定 3.1.2的版本真的很古老 解决 增加 ...
- 解决:docker-compose端口绑定
docker-compose 进程绑定 Bind for 0.0.0.0:3825 failed: port is already allocated 查看进程发现有进程在关闭后继续进行 docker ...
- javascript入门 之 ztree (六 结点的点击和展开/折叠事件)
1.注意: 测试点击事件时,如果要测试取消选中和追加选中,如果按住ctrl和win键无用,则需要先用鼠标左键按住,然后,在松开左键的前几毫秒按住ctrl键便可! <!DOCTYPE html&g ...
- 自动生成四则运算题目(C语言)
Github项目地址:https://github.com/huihuigo/expgenerator 合作者:马文辉(3118005015).卢力衔(3118005013) 项目简介 1题目:实现一 ...
- "字母全变大写"组件:<uppercase> —— 快应用组件库H-UI
 <import name="uppercase" src="../Common/ui/h-ui/text/c_text_uppercase">& ...
- "小号文本"组件:<small> —— 快应用组件库H-UI
 <import name="small" src="../Common/ui/h-ui/text/c_tag_small"></impor ...
- java课程设计之--Elasticsearch篇
一.团队课程设计博客链接 https://www.cnblogs.com/Rasang/p/12169899.html 二.个人负责模块或任务说明 2.1Elasticsearch简介 Elastic ...
- Laravel路由不生效,除了首页全部404解决方案Nginx环境
原因: 请求根目录/ (http://www.xxx.com/public/),会请求public/index.php 输入其他路由地址时,会把你的请求定位到:http://www.xxx.com/i ...