PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
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 (≤) which is the total number of nodes, and a positive K (≤) 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
题意:
给N个链表结点,以及K,对每K个长度的链表做逆置,输出逆置后的链表。
题解:
不是很熟悉vector.reverse()这种工具,用了两个双端队列和一个栈来实现。最后一个测试点没过,原来是有些节点不在链表上,那么需要重新计算节点个数,加个计数器sum,不一定就是n。

AC代码:
#include<iostream>
#include<algorithm>
#include<deque>
#include<stack>
using namespace std;
struct node{
int v;
int zhi;
int nx;
}a[];
deque<node>q1,q2;
stack<node>st;
int main(){
int root,n,k;
cin>>root>>n>>k;
for(int i=;i<=n;i++){
int x;
cin>>x;
cin>>a[x].v>>a[x].nx;
a[x].zhi=x;//把它自己的编号也要记录下来
}
int sum=;//可能有些节点不在链表上,要重新数
int p=root;
q1.push_back(a[p]);
sum++;
while(a[p].nx!=-){
int next=a[p].nx;
q1.push_back(a[next]);
sum++;
p=next;
}
for(int i=;i<=sum/k;i++){
int c=;
while(!q1.empty()){//k个k个分别装入栈里倒一倒再取出来
node x=q1.front();
q1.pop_front();
st.push(x);
c++;
if(c==k) break;
}
while(!st.empty()){//倒着再取出来
q2.push_back(st.top());
st.pop();
}
}
while(!q1.empty()){
node x=q1.front();
q1.pop_front();
q2.push_back(x);
}
while(!q2.empty()){//输出
node x=q2.front();
q2.pop_front();
if(!q2.empty()) printf("%05d %d %05d\n",x.zhi,x.v,q2.front().zhi);
else printf("%05d %d -1",x.zhi,x.v);
}
return ;
}
别人的代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+;
struct node{
int add,data,Next;
}a[maxn];
vector<node>valid,ans;
int head,n,k;
int main()
{
for(int i=;i<maxn;i++)a[i].add=i;
scanf("%d%d%d",&head,&n,&k);
for(int i=;i<n;i++)
{
int address;
scanf("%d",&address);
scanf("%d%d",&a[address].data,&a[address].Next);
}
int p=head;
while(p!=-)
{
valid.push_back(a[p]);
p=a[p].Next;
}
int group=valid.size()/k;
for(int i=;i<group;i++)
{
reverse(valid.begin()+i*k,valid.begin()+i*k+k);
}
for(int i=;i<valid.size();i++)
{
if(i!=valid.size()-)printf("%05d %d %05d\n",valid[i].add,valid[i].data,valid[i+].add);
else printf("%05d %d -1\n",valid[i].add,valid[i].data);
}
return ;
}
PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)的更多相关文章
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...
- 【PAT甲级】1074 Reversing Linked List (25 分)
题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...
- PAT Advanced 1074 Reversing Linked List (25) [链表]
题目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K e ...
- 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 ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 甲级 1062 Talent and Virtue (25 分)(简单,结构体排序)
1062 Talent and Virtue (25 分) About 900 years ago, a Chinese philosopher Sima Guang wrote a histor ...
随机推荐
- LoadRunner 11的破解方法和license号
安装过程中遇到“命令行选项语法错误键入命令 \?获得帮助”2005的安装问题,可参考本文:http://www.cnblogs.com/lelexiong/p/8974149.html解决 破解方法: ...
- Beta:凡事预则立
课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺) 团队名称:葫芦娃队 作业目标:尽力完成 团队博客 队员学号 队员昵称 博客地址 041602421 der himmel ht ...
- (尚021)Vue_eslint编码规范检查
1.eslint 1.1说明 1)ESLint是一个代码规范检查工具 2)它定义了很多特定的规则,一旦你的代码违背了某一规则,eslint会做出非常有用的提示 3)官网:http://eslint.o ...
- python(三)——while语句
while死循环 #!/usr/bin/env python #-*- coding:utf8 -*- import time while 1 == 1: print('Ok',time.time() ...
- Phalcon框架的编译安装 内存不足的解决办法
对症解决 有两种解决方法,一种是提升ECS系统内存.但是却要真金白银跟阿里云去购买的.另一种,则是手动创建swap交换文件.下面来介绍第二种方法. 第一步:首先确定系统是否已经开启swap交换分区: ...
- cyyz : Day 1 数论整理
声明:感谢修改这篇博客的dsr Day 1 先说一下上午的听课吧,哎~,简直了,简直(⊙o⊙)…咋说呢,引人入胜???No! 是昏昏欲睡好吧...一点听课欲都没有(强撑....),一上午停下来简直怀疑 ...
- 洛谷 P3905 道路重建 题解
P3905 道路重建 题目描述 从前,在一个王国中,在\(n\)个城市间有\(m\)条道路连接,而且任意两个城市之间至多有一条道路直接相连.在经过一次严重的战争之后,有\(d\)条道路被破坏了.国王想 ...
- last-child为啥不生效
last-child基本定义 指定属于其父元素的最后一个子元素的 p 元素的背景色 如果你这样写是不会生效的 <div class="limit-border"> &l ...
- 配置Always On AG
1.准备测试环境的服务器 在 Always On AG 中如果需要自动 Failover 至少需要集群中有 3 台服务器,但是我只是测试功能,因此只使用了两台服务器.并且本文不涉及任何 Pacemak ...
- python 操作redis,存取为字节格式,避免转码加
这种情况连接数据库,对数据的存取都是字节类型,存取时还得转码一下 from redis import Redis # 实例化redis对象 rdb = Redis(host='localhost', ...