当前位置:网站首页>Array analog linked list
Array analog linked list
2022-07-27 16:33:00 【The corners of the mouth rise*】
4273. List merge
Given two single chain tables L1=a1→a2→…→an−1→an and L2=b1→b2→…→bm−1→bm.
If n≥2m, Your task is to reverse the shorter list , Then merge it into a longer linked list , Get the shape of a1→a2→bm→a3→a4→bm−1… Result .
For example, give two linked lists as 6→7 and 1→2→3→4→5, You should output 1→2→7→3→4→6→5.
Input
Input first gives two linked lists in the first row L1 and L2 The address of the header node of , And positive integers N, That is, the total number of nodes given .
The address of a node is 5 Nonnegative integer of digits ( May contain leading 0), Empty address NULL use −1 Express .
And then 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 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 .
1≤N≤105
Output
Output the result linked list in order , Each node occupies a row , The format is the same as the input .
The 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
AC Code
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int e[N],ne[N];
int main(){
int h1,h2,n;
cin >> h1 >> h2 >> n;
while(n--){
int add, data, next;
cin >> add >> data >> next;
e[add] = data;
ne[add] = next;
}
vector<pair<int , int >> a, b;
for(int i = h1; i != -1 ; i = ne[i])
a.push_back(make_pair(i,e[i]));
for(int i = h2; i != -1; i = ne[i])
b.push_back(make_pair(i,e[i]));
vector<pair<int ,int>> c;
if(a.size() < b.size()) swap(a,b);
for(int i = 0 , j = b.size() - 1; i < a.size(); i += 2, j--){
c.push_back(a[i]);
if(i + 1 < a.size()) c.push_back(a[i + 1]);
if(j >= 0) c.push_back(b[j]);
}
for(int i = 0; i < c.size(); i++){
printf("%05d %d ",c[i].first,c[i].second);
if(i + 1 < c.size())
printf("%.5d\n",c[i + 1].first);
else
puts("-1");
}
return 0;
}
边栏推荐
猜你喜欢

【论文阅读】Transformer with Transfer CNN for Remote-Sensing-ImageObject Detection

(二)动态卷积之Dynamic Convolution

Product axure9 English version, using repeater repeater to realize drop-down multi selection box

Cron expression use

Opencv (I) -- basic knowledge of image

Draw circuit diagram according to Verilog code

The 21st - -- the 30th time

201403-1

HowNet and Wanfang database download papers for free ----- several times faster than connecting to the school intranet (some schools Wanfang database does not support downloading)

Yys mouse connector
随机推荐
Gpt-2 text generation
HowNet and Wanfang database download papers for free ----- several times faster than connecting to the school intranet (some schools Wanfang database does not support downloading)
OpenCV(二)——图像基本处理
大数相加
2021-06-02
MySQL high version report SQL_ mode=only_ full_ group_ By exception
201403-1
Characters generated by JMeter function assistant in jmeter5.3 and later versions cannot be copied when they are grayed out
Implementation of filler creator material editing tool
TP5 query empty two cases
ARIMA model selection and residuals
IO流简介
google chrome revercecaptcha广告屏蔽
Const summary
Embedded interview
Find active SQL connections in SQL Server
DEX and AMMS of DFI security
Log management
web测试学习笔记01
*List reversal