【PAT甲级】1074 Reversing Linked List (25 分)
题意:
输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址。输出每K个结点局部反转的链表。
trick:
测试点6包含一些不在起点这条链表上的结点。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
map<string,pair<int,string> >mp;
pair<string,int>ans[];
pair<string,int>ans2[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s;
int n,k;
cin>>s>>n>>k;
for(int i=;i<=n;++i){
string s1,t1;
int x;
cin>>s1>>x>>t1;
mp[s1]={x,t1};
}
int cnt=;
while(){
string now=s;
ans[++cnt]={now,mp[now].first};
s=mp[now].second;
if(s=="-1")
break;
}
int pos=;
for(int i=;i*k<=cnt;++i){
pos=i*k;
for(int j=i*k-k+;j<=i*k;++j)
ans2[j]=ans[(*i-)*k+-j];
}
for(int i=pos+;i<=cnt;++i)
ans2[i]=ans[i];
for(int i=;i<=cnt;++i){
cout<<ans2[i].first<<" "<<ans2[i].second<<" ";
if(i<cnt)
cout<<ans2[i+].first;
else
cout<<-;
cout<<"\n";
}
return ;
}
【PAT甲级】1074 Reversing Linked List (25 分)的更多相关文章
- 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 ...
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于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 ...
随机推荐
- Reverse is Multiplex, You Need PinTools.
Read this slide: pin_in_CTF.pdf And this link: pin_in_CTF
- 正则表达式过滤url请求
过滤url中带reset的url请求 atgBusSignFilter.setSignUriRegex("^.*/reset/.*$")等价于 atgBusSignFilter.s ...
- 1.Java多线程之wait和notify
1.首先我们来从概念上理解一下这两个方法: (1)obj.wait(),当obj对象调用wait方法时,这个方法会让当前执行了这条语句的线程处于等待状态(或者说阻塞状态),并释放调用wait方法的对象 ...
- python 日志模块 日志格式
形如: formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s","%Y%b%d-%H: ...
- bzoj 4196:[NOI2015] 软件包管理器 (树链剖分)
第一次做树剖 找同学要了模板 + 各种借鉴 先用dfs在划分轻重链并编号, install的时候就从查询的节点到根寻找标记的点有多少个,再用深度减去标记的点的个数,并把路径上所有点都标记 uninst ...
- 安装pecl
$ wget http://pear.php.net/go-pear.phar $ php go-pear.phar //php版本 < 7 $ yum install php-pear // ...
- 《E=MC2或一个思想的故事》
思想是起点.一切行动都以萌芽状态孕藏在思想之中,以往所做过的一切均离不开思想. 他是个纯朴的人,喜欢在乡下静静地冥想. .而科学家们却非常清楚,那些最伟大的成就都是在静默中完成的.
- apache+SSL 搭建https
简单介绍 一般情况下,我们打开网站默认的是使用明文传输方式,但在日常生活中,当我们在登录或者支付交易时,网站就会自动跳转至SSL(Secure Sockets Layes)加密传输模式,SSL的功能就 ...
- C语言数据结构——第二章 线性表
二.线性表 2.1-线性表简介 2.1.1-线性表的定义 线性表是由若干个相同特性的数据元素组成的有限序列.若该线性表不包含任何元素,则称为空表,此时长度为0,当线性表不为空时,表中的元素的个数就是线 ...
- const在C与C++中的区别
在C中,const不是常量,只能说是一个不能改变的变量(注意是变量),C编译器不能把const看成看成一个编译期间的常量,因为他在内存中有分配,C编译器不知道他在编译期间的值.所以不能作为数组定义时的 ...