当前位置:网站首页>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;
}
边栏推荐
- Sudden! 28 Chinese entities including Hikvision / Dahua / Shangtang / Kuangshi / ITU / iFLYTEK have been blacklisted by the United States
- The whereor method of TP5 has many conditions
- SolidWorks Simulation曲线图属性设定
- Test novice learning classic (with ideas)
- 【论文阅读】Transformer with Transfer CNN for Remote-Sensing-ImageObject Detection
- The method of inserting degree in word
- t5 学习
- Google Chrome reversecaptcha ad blocking
- Characters generated by JMeter function assistant in jmeter5.3 and later versions cannot be copied when they are grayed out
- Boolean value
猜你喜欢

Opencv (II) -- basic image processing

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

DEX and AMMS of DFI security

The whereor method of TP5 has many conditions

Opencv (V) -- moving target recognition

CCF-201312-1

Time series ARIMA model

字节跳动服务网格基于 Hertz 框架的落地实践

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)

知网、万方数据库免费下载论文------比连接学校内网速度快数倍不止(有的学校万方数据库不支持下载)
随机推荐
JSP Foundation
第31回---第52回
Script file ‘D:\programs\anaconda3\Scripts\pip-script.py‘ is not present.
Leetcode25 question: turn the linked list in a group of K -- detailed explanation of the difficult questions of the linked list
Brief description of tenant and multi tenant concepts in cloud management platform
大数相加
2021-06-02
Flowable process custom attribute
Oracle 常用语句
Test novice learning classic (with ideas)
CCF-201312-1
jupyter 创建虚拟环境并安装pytorch(gpu)
The new JMeter function assistant is not under the options menu - in the toolbar
Rotate string left
201403-1
论文缩小
training on multiple GPUs pytorch
Addition of large numbers
OpenCV(五)——运动目标识别
Find active SQL connections in SQL Server