当前位置:网站首页>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]);
}
}
边栏推荐
猜你喜欢
随机推荐
软件测试周刊(第81期):能够对抗消极的不是积极,而是专注;能够对抗焦虑的不是安慰,而是具体。
ERP管理系统在装备制造企业管理中的应用
C语言宏定义
What is the difference between server hosting and virtual host
AWS篇1
[hiflow] regularly send Tencent cloud SMS sending group
ClickHouse,让查询飞起来!!!
day1
超详细MP4格式分析
没有了华为,高通任意涨价,缺乏核心技术的国产手机只能任由宰割
The landing process of 800V high-voltage fast charging was accelerated, and Junsheng Electronics was designated for the 500million euro project
PHP代码审计4—Sql注入漏洞
STL deque
Jsd-2204 session management filter day19
[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~
800V高压快充落地进程加快均胜电子获5亿欧元项目定点
《快速掌握QML》第四章 事件处理
Conda设置代理
C语言注释的方法
Part III detailed explanation of RBAC authority management database design


![[200 opencv routines] 225. Fourier descriptor for feature extraction](/img/4b/1f373505ffd5c0dbaa5c20431c4b42.png)






