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. post登录 jsessionid 以及cookie 传递

    先配置登录接口请求 登录成功后: 再其它请求中设置

  2. django禁用csrf

    django禁用csrf 函数试图 from django.views.decorators.csrf import csrf_exempt @csrf_exempt def your_func_vi ...

  3. 2017蓝桥杯购物单(C++B组)

    原题: 标题: 购物单 小明刚刚找到工作,老板人很好,只是老板夫人很爱购物.老板忙的时候经常让小明帮忙到商场代为购物.小明很厌烦,但又不好推辞.这不,XX大促销又来了!老板夫人开出了长长的购物单,都是 ...

  4. 单线程IP扫描解析

    扫描代码: private void Button_Click(object sender, RoutedEventArgs e) { a5.Items.Clear(); string str = t ...

  5. matplotlib TransformedBbox 和 LockableBbox

    TransformedBbox 和 LockableBbox 都是BboxBase的子类.TransformedBbox支持使用变换来初始化bbox, LockableBbox可实现锁定bbox的边不 ...

  6. git处理fork的个人库代码与远程项目库待代码同步与合并

    由于每个项目组git代码管理规范都不一致,分支开发和fork开发模式.我就说下fork处理的流程吧. 问题点: 我的代码是从自己的远程个人仓库clone的.我的个人远程仓库代码是从项目的远程仓库弄fo ...

  7. app扫描二维码登陆

    先说明一下实现原理: 如同微信扫描登陆一样,就是一种pc的自动登陆授权.在网站首页得有切换登陆的选项:密码登陆 扫码登陆 当用户切换到扫码登陆时,向服务器请求一次获得一个唯一的uukey 利用这个uu ...

  8. 数据结构和算法(Golang实现)(12)常见数据结构-链表

    链表 讲数据结构就离不开讲链表.因为数据结构是用来组织数据的,如何将一个数据关联到另外一个数据呢?链表可以将数据和数据之间关联起来,从一个数据指向另外一个数据. 一.链表 定义: 链表由一个个数据节点 ...

  9. Gatling脚本编写技巧篇(一)

    一.公共类抽取 熟悉Gatling的同学都知道Gatling脚本的同学都知道,Gatling的脚本包含三大部分: http head配置 Scenario 执行细节 setUp 组装 那么针对三部分我 ...

  10. 开始appium的第一个脚本

    设置DesiredCapabilities 存在于以下库中: org.openqa.selenium.remote.DesiredCapabilities Desired Capabilities告诉 ...