当前位置:网站首页>List merging (summer vacation daily question 3)
List merging (summer vacation daily question 3)
2022-07-23 15:49:00 【sweetheart7-7】
Given two single chain tables L 1 = a 1 → a 2 → … → a n − 1 → a n L_1=a_1→a_2→…→a_{n−1}→a_n L1=a1→a2→…→an−1→an and
L 2 = b 1 → b 2 → … → b m − 1 → b m L_2=b_1→b_2→…→b_{m−1}→b_m L2=b1→b2→…→bm−1→bm.
If n ≥ 2 m n≥2m n≥2m, Your task is to reverse the shorter list , Then merge it into a longer linked list , Get the shape of a 1 → a 2 → b m → a 3 → a 4 → b m − 1 … a_1→a_2→b_m→a_3→a_4→b_{m−1}… a1→a2→bm→a3→a4→bm−1… Result .
For example, give two linked lists as 6 → 7 6→7 6→7 and 1 → 2 → 3 → 4 → 5 1→2→3→4→5 1→2→3→4→5, You should output 1 → 2 → 7 → 3 → 4 → 6 → 5 1→2→7→3→4→6→5 1→2→7→3→4→6→5.
Add
This question may contain nodes that are not in the two single linked lists , These nodes need not be considered .
Input format
Input first gives two linked lists in the first row L 1 L1 L1 and L 2 L2 L2 The address of the header node of , And positive integers N N N, That is, the total number of nodes given .
The address of a node is 5 5 5 Nonnegative integer of digits ( May contain leading 0 0 0), Empty address NULL use −1 Express .
And then N N N That's ok , Each line gives the information of a node in the following format :
Address Data Next
among Address Is the address of the node ,Data No more than 1 0 5 10^5 105 The positive integer ,Next Is the address of the next node .
The title ensures that there is no empty linked list , And the longer list is at least twice as long as the shorter list .
Output format
Output the result linked list in order , Each node occupies a row , The format is the same as the input .
Data range
1 ≤ N ≤ 1 0 5 1≤N≤10^5 1≤N≤105
sample input :
00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1
sample output :
01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 1000010;
int n, h1, h2;
int e[N], ne[N];
int main(){
cin >> h1 >> h2 >> n;
int address, data, neaddress;
for(int i = 0; i < n; i++){
cin >> address >> data >> neaddress;
e[address] = data;
ne[address] = neaddress;
}
vector<int> v1, v2, v3;
for(int i = h1; ~i; i = ne[i])
v1.push_back(i);
for(int i = h2; ~i; i = ne[i])
v2.push_back(i);
if(v1.size() < v2.size()) swap(v1, v2);
reverse(v2.begin(), v2.end());
for(int i = 0; i < v1.size(); i++){
v3.push_back(v1[i]);
if(i % 2 && i / 2 < v2.size()) v3.push_back(v2[i/2]);
}
for(int i = 0; i < v3.size(); i++){
printf("%05d %d ", v3[i], e[v3[i]]);
if(i == v3.size() - 1) puts("-1");
else printf("%05d\n", v3[i+1]);
}
}
边栏推荐
- Part III detailed explanation of RBAC authority management database design
- STL deque
- Redis的过期策略以及内存淘汰机制,Key过期了为什么没释放内存
- Redis 删除Key命令会导致阻塞么?
- Batch deletion with RPM -e --nodeps
- Can multithreading optimize program performance?
- 对C语言最基本的代码解释
- C语言经典例题-两个分数相加
- 工业物联网中的时序数据
- Kirin V10 source code compilation qtcreater4.0.3 record
猜你喜欢

Idea five free plug-ins to improve efficiency
![[pyGame actual combat] aircraft shooting masterpiece: fierce battle in the universe is imminent... This super classic shooting game should also be taken out and restarted~](/img/a3/087b1bc7445c53ddbd6d7334da51b9.png)
[pyGame actual combat] aircraft shooting masterpiece: fierce battle in the universe is imminent... This super classic shooting game should also be taken out and restarted~

946. 验证栈序列 ●● & 剑指 Offer 31. 栈的压入、弹出序列 ●●

C语言经典例题-商品检验码

Completely uninstall MySQL in centos7

BGP federal experiment

Application of ERP management system in equipment manufacturing enterprise management

自定义封装弹出框(带进度条)

printf函数-转换说明

Les raccourcis clavier liés à l'onglet ne peuvent pas être utilisés après la mise à jour du vscode
随机推荐
软件测试周刊(第81期):能够对抗消极的不是积极,而是专注;能够对抗焦虑的不是安慰,而是具体。
在一个有序数组中查找具体的某个数字(二分查找or折半查找)
Jsd-2204 session management filter day19
946. 验证栈序列 ●● & 剑指 Offer 31. 栈的压入、弹出序列 ●●
BGP联邦实验
C语言经典例题-用4×4矩阵显示从1到16的所有整数,并计算每行、每列和每条对角线上的和
[heuristic divide and conquer] the inverse idea of heuristic merging
【攻防世界WEB】难度三星9分入门题(下):shrine、lottery
Find the source code of the thesis
【Pygame实战】打扑克牌嘛?赢了输了?这款打牌游戏,竟让我废寝忘食。
一个悄然崛起的国产软件,太强了!
Part V Druid data source introduction
day1
C # calculate the number of times a character appears in the string
ERP管理系统在装备制造企业管理中的应用
md5强碰撞,二次解码,
PHP代码审计4—Sql注入漏洞
Camera flashlight modification
Redis中 LRU和LFU的淘汰策略区别
STL map attribute