02-线性结构3 Reversing Linked List(25 point(s))

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 (≤10​5​​) which is the total number of nodes, and a positive K (≤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 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

思路

用 STL 模拟 链表

然后 要注意

可能会有 多余结点 不在链表上面

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7; struct Node
{
int add;
int value;
int next;
void read()
{
scanf("%d%d%d", &add, &value, &next);
}
}q[maxn]; vector <Node> v, ans; map <int, Node> m; void dfs(int add)
{
v.pb(m[add]);
if (m[add].next != -1)
dfs(m[add].next);
} int main()
{
int start, n, k;
scanf("%d%d%d", &start, &n, &k);
for (int i = 0; i < n; i++)
{
q[i].read();
m[q[i].add] = q[i];
}
dfs(start);
n = v.size();
int len = v.size() / k;
for (int i = 1; i <= len; i++)
{
int j = i * k - 1;
int m = k;
while (m--)
ans.pb(v[j--]);
}
for (int i = len * k; i < n; i++)
ans.pb(v[i]);
for (int i = 0; i < n - 1; i++)
printf("%05d %d %05d\n", ans[i].add, ans[i].value, ans[i + 1].add);
printf("%05d %d -1\n", ans[n - 1].add, ans[n - 1].value);
}

02-线性结构3 Reversing Linked List(25 point(s)) 【链表】的更多相关文章

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

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

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

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

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

  5. PAT甲题题解-1074. Reversing Linked List (25)-求反向链表

    题意说的很清楚了,这种题的话,做的时候最好就是在纸上自己亲手模拟一下,清楚一下各个指针的情况, 这样写的时候就很清楚各个指针变量保存的是什么值. PS:一次AC哈哈,所以说自己动手在纸上画画还是很有好 ...

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

    02-线性结构3 Reversing Linked List   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...

  7. PAT1074 Reversing Linked List (25)详细题解

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

  8. PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)

    1074 Reversing Linked List (25 分)   Given a constant K and a singly linked list L, you are supposed ...

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

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

随机推荐

  1. Python Challenge 第四关

    进入了第四关.只有一张图,我还是像往常一样查看源代码.果然,发现了一行注释:urllib may help. DON'T TRY ALL NOTHINGS, since it will never e ...

  2. AC日记——城市 洛谷 P1401

    题目描述 N(2<=n<=200)个城市,M(1<=m<=40000)条无向边,你要找T(1<=T<=200)条从城市1到城市N的路,使得最长的边的长度最小,边不能 ...

  3. 洛谷——P2983 [USACO10FEB]购买巧克力Chocolate Buying

    P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...

  4. Mac item2 配色,大小写敏感及常用快捷键

    http://blog.csdn.net/lainegates/article/details/38313559 目录(?)[-] 配色 大小写敏感 快捷揵   item2是mac下非常好用的一款终端 ...

  5. fastscript增加公共函数

    fastscript增加公共函数 unit fs_BsCommFuncs; interface{$i fs.inc}uses SysUtils, Classes, fs_iclassesrtti, f ...

  6. Win7下Nginx的安装与配置

    1.  下载nginx1.9.9版本:(版本随时间而变,下载最新即可) http://nginx.org/download/nginx-1.9.9.zip 2. 解压软件到对应位置,并重命名文件夹为n ...

  7. sublime快捷键设置

    1.sublime自带的快捷键设置:这里的super在mac下是指command键 右边的内容表示用户自定义的快捷键:比如如图所示command+d表示复制光标所在行 常用操作:复制粘贴什么的太常用的 ...

  8. 详解RocketMQ中的consumer

    上述就是MQ中有关Consumer的类图,下面来介绍一下每个类 1.MQAdmin:底层类,上篇博客已经提过,就不再此重提 2.MQConsumer:Consumer公共的接口,常用的方法如下 如果消 ...

  9. Wish3D用户必看!模型加载失败原因汇总

    上传到Wish3D的模型加载不出来,作品显示页面漆黑一片,是什么原因? 很有可能是操作过程中的小失误,不妨从以下几点检查.还是不行的请加QQ群(Wish3D交流群3):635725654,@Wish3 ...

  10. OpenCV学习教程入门篇&lt;一、介绍&gt;

    OpenCV,是Inter公司开发的免费开源专门因为图像处理和机器视觉的C/C++库,英文全称是Open Source Computer Vision. 1. 可视化语言Matlab与OpenCV都能 ...