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≤10​5​​) 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的更多相关文章

  1. pat02-线性结构1. Reversing Linked List (25)

    02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...

  2. 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 ...

  3. 02-线性结构2 Reversing Linked List

    由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are ...

  4. 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 ...

  5. 数据结构练习 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. PAT02-线性结构3 Reversing Linked List

    题目:https://pintia.cn/problem-sets/1010070491934568448/problems/1037889290772254722 先是看了牛客(https://ww ...

随机推荐

  1. SSAS 第一篇:多维数据分析基础

    多维数据分析是指按照多个维度(即多个角度)对数据进行观察和分析,多维的分析操作是指通过对多维形式组织起来的数据进行切片 .切块.聚合.钻取 .旋转等分析操作,以求剖析数据,使用户能够从多种维度.多个侧 ...

  2. 微信号网页版api

    Django Wechat Api djangowechatapi是基于wxpy和django制作的web应用 安装 使用pip pip install djangowechatapi 源码安装 gi ...

  3. C++11中的四种类型转换

    static_cast 基础数据类型转换(基本类型) 同一继承体系中类型的转换(父子类型) 任意类型与空指针(void *)之间的转换(指针类型) dynamic_cast 执行派生类指针或引用与基类 ...

  4. C++语言实现顺序表

    C++语言实现顺序表 顺序表的定义及其特点 顺序表的定义是:把线性表中的所有表项按照其逻辑顺序依次存储到从计算机存储中指定存储位置开始的一块连续的存储空间中. 这样,线性表中第一个表项的存储位置就是被 ...

  5. 萌新带你开车上p站(Ⅳ)

    本文作者:萌新 前情回顾: 萌新带你开车上p站(一) 萌新带你开车上p站(二) 萌新带你开车上P站(三) 回顾一下前篇,我们开始新的内容吧 0x12 登录后看源码 通读程序,逻辑是这样子的: 输入6个 ...

  6. 架构师修炼之设计模式 - 策略模式(Strategy) 【Python与C#实现】

    程序员,我为你祝福 愿你有一个灿烂的前程 愿你有情人终成眷属 愿你在尘世获得幸福 我只想成为架构师,走遍江湖! 目录 模式定义 模式分类 模式结构 实例(C#与Python版) 优点 缺点 使用场景 ...

  7. CTR学习笔记&代码实现3-深度ctr模型 FNN->PNN->DeepFM

    这一节我们总结FM三兄弟FNN/PNN/DeepFM,由远及近,从最初把FM得到的隐向量和权重作为神经网络输入的FNN,到把向量内/外积从预训练直接迁移到神经网络中的PNN,再到参考wide& ...

  8. G - Greg and Array CodeForces - 296C 差分+线段树

    题目大意:输入n,m,k.n个数,m个区间更新标记为1~m.n次操作,每次操作有两个数x,y表示执行第x~y个区间更新. 题解:通过差分来表示某个区间更新操作执行的次数.然后用线段树来更新区间. #i ...

  9. 谁说 Vim 不好用?送你一个五彩斑斓的编辑器!

    相信大家在使用各种各样强大的 IDE 写代码时都会注意到,代码中各种类型的关键字会用独特的颜色标记出来,然后形成一套语法高亮规则.这样不仅美观,而且方便代码的阅读. 而在上古神器 Vim 中,我们通常 ...

  10. [html]浏览器标签小图标LOGO简单设置

    方式一:如果是一个项目一个LOGO 的话,直接接把图片像素设置成16x16像素,然后改名favicon.ico放在项目根部目录就可以,自动识别的! 方式二:简单设置! 首先找一个图片把像素设置成16x ...