AcWing 4273. 链表合并
原题链接
简单
C++ 代码
#include <iostream>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
struct jd{
string cur;
int val;
string next;
}temp;
const int N = 1e5 + 10;
int n, idx;
vector<string> l1(1), l2(1);
jd mp[N];
int main()
{
cin >> l1[0] >> l2[0] >> n;
while (n --)
{
cin >> temp.cur >> temp.val >> temp.next;
mp[stoi(temp.cur)] = temp;
}
while (mp[stoi(l1.back())].next != "-1")
l1.push_back(mp[stoi(l1.back())].next);
while (mp[stoi(l2.back())].next != "-1")
l2.push_back(mp[stoi(l2.back())].next);
if (l1.size() < l2.size()) l1.swap(l2);
l1.push_back("-1");
idx = l2.size() - 1;
for (int i = 0; i < l1.size() - 1; ++ i)
{
cout << l1[i] << ' ' << mp[stoi(l1[i])].val << ' ';
if ((i + 1) % 2 == 0 && idx >= 0)
{
cout << l2[idx] << endl;
cout << l2[idx] << ' ' << mp[stoi(l2[idx --])].val << ' ' << l1[i + 1] << endl;
}
else cout << mp[stoi(l1[i])].next << endl;
}
return 0;
}