当前位置: 首页 > news >正文

【PTA Data Structures and Algorithms (English)】7-2 Reversing Linked List

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.

题意:

给你一个链表和一个数K,然后你需要做的就是每K个节点作为一组进行反向,最后输出操作完之后的链表

思路:

直接大模拟,STL用起来就完事,不用考虑复杂度,下面看代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
map<string,int> mp;
string invmp[N];
int cnt = 0;
struct node{string a,c;int b;
}Node[N];
struct Edge{int da;int next;
}da[N];
struct ttt{string l,r;int da;
};
int main(){int n,k;string root;ios::sync_with_stdio(false);cin.tie(0);cin>>root>>n>>k;mp[root] = ++cnt;invmp[cnt] = root;for(int i=1;i<=n;i++) da[i].next = 0;for(int i=1;i<=n;i++){cin>>Node[i].a>>Node[i].b>>Node[i].c;if(!mp[Node[i].a]) mp[Node[i].a] = ++cnt,invmp[cnt] = Node[i].a;if(!mp[Node[i].c]) mp[Node[i].c] = ++cnt,invmp[cnt] = Node[i].c;if(Node[i].c == "-1") mp[Node[i].c] = 0;da[mp[Node[i].a]].next = mp[Node[i].c];da[mp[Node[i].a]].da = Node[i].b;}invmp[0] = "-1";int p = mp[root];vector<ttt> ans;while(p != 0){vector<ttt> a;for(int i=1;i<=k;i++){if(p == 0){for(auto t : a) ans.push_back(t);for(int i=0;i<ans.size()-1;i++) ans[i].r = ans[i+1].l;ans[ans.size()-1].r = "-1";for(auto t : ans){cout<<t.l<<" "<<t.da<<" "<<t.r<<"\n";} return 0;}ttt t = {invmp[p],invmp[da[p].next],da[p].da};a.push_back(t);p = da[p].next;
//			cout<<invmp[p]<<" dasf\n";}reverse(a.begin(),a.end());for(auto t : a) ans.push_back(t);}for(int i=0;i<ans.size()-1;i++) ans[i].r = ans[i+1].l;ans[ans.size()-1].r = "-1";for(auto t : ans){cout<<t.l<<" "<<t.da<<" "<<t.r<<"\n";}return 0;
}
http://www.lryc.cn/news/36561.html

相关文章:

  • Jetpack Compose 学习汇总
  • 【OpenCv】c++ 图像初级操作 | 图像灰度化
  • VIT(vision transformer)onnx模型解析
  • 红黑树的介绍和实现
  • C/C++每日一练(20230310)
  • Go语言基础知识
  • 案例06-没有复用思想的接口和sql--mybatis,spring
  • 如何将项目部署到服务器:从选择服务器到维护应用程序的全流程指南
  • 怎么做才能不丢消息?
  • 前端基础(十六)_数组对象
  • 数据结构-带头双向循环链表
  • 3 问 6 步,极狐GitLab 帮助企业构建高效、安全、合规的 DevSecOps 文化
  • SPA(单页应用)知多少
  • Selenium实战【远程控制】【JAVA爬虫】
  • 图片动画化应用中的动作分解方法
  • 我又和redis超时杠上了
  • 一文带你吃透MySQL数据库!
  • [学习笔记] 2. 数据结构
  • [学习笔记] 3. 算法进阶
  • 做自媒体真的能赚到钱吗?真的能赚到几十万吗?
  • QT使用QListWidget显示多张图片
  • python 打印进度条
  • 【微小说】大学日记
  • ArrayList扩容机制解析
  • jsp-----web应用与开发
  • 洛谷 P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers
  • php设计模式-组合模式的运用
  • 一文教会你如何简单使用Fegin进行远程服务调用
  • OpenAI——CLIPs(代码使用示例)
  • 什么样的人更适合创业?那类人创业更容易成功?