当前位置:网站首页>L2-002 链表去重(测试点1的坑)
L2-002 链表去重(测试点1的坑)
2022-04-22 06:16:00 【S atur】


思路: 通过map映射先将原链表处理出来,再通过判重将其分成两部分存储输出。注意测试点1有个坑点(原链表不是一条完成的链表,数据如下:)
(测试点1)输入:
00001 3
00001 1 00002
00002 2 -1
00003 3 00004
(测试点1)输出:
00001 1 00002
00002 2 -1
代码实现:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5+10;
int n, b[N], idx, tt, val;
string head, pos, nxt;
map<int, int> vis;
map<string, int> mp;
map<string, string> np;
vector<string> a, ans1, ans2;
int a1[N], a2[N], tt1, tt2, last;
//struct node{
// int pos, val, next;
//}b[N];
signed main()
{
cin >> head >> n;
for(int i = 1; i <= n; i ++){
cin >> pos >> val >> nxt;
mp[pos] = val;
np[pos] = nxt;
if(pos==head) idx = i;
}
a.push_back("0");
while(tt<n){
a.push_back(head);
b[++tt] = mp[head];
head = np[head];
}
for(int i = 1; i <= n; i ++){
// cout << a[i] << " \n"[i==n];
if(a[i]=="-1"){
last = i-1;
break;
}
}
if(!last) last = n; //测试点1的坑
// cout << "last: " << last << endl;
for(int i = 1; i <= last; i ++){
// cout << b[i] << " \n"[i==n];
if(!vis[abs(b[i])]){
vis[abs(b[i])] = 1;
ans1.push_back(a[i]);
a1[tt1++] = b[i];
}
else{
ans2.push_back(a[i]);
a2[tt2++] = b[i];
}
}
for(int i = 0; i < tt1; i ++){
if(i==tt1-1) cout << ans1[i] << " " << a1[i] << " " << -1 << endl;
else cout << ans1[i] << " " << a1[i] << " " << ans1[i+1] << endl;
}
for(int i = 0; i < tt2; i ++){
if(i==tt2-1) cout << ans2[i] << " " << a2[i] << " " << -1 << endl;
else cout << ans2[i] << " " << a2[i] << " " << ans2[i+1] << endl;
}
return 0;
}
版权声明
本文为[S atur]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Satur9/article/details/124318441
边栏推荐
- Anaconda安装与使用
- Pycharm only needs five steps to improve the download speed by using Tsinghua image source
- Prompt the user to enter his name, and the user will write his name to the file guest Txt program determines that when it is not equal to N, it executes to create the file data Txt, a total of 100000
- SQL Server quick start
- [number theory] prime number (V): Mason prime number (lucas_lehmer decision method)
- 1420 · 最小覆盖子串II
- 快排与归并排序
- The only storage area in the JVM where GC and oom will not occur
- 双向循环链表(详)
- 队列(详解)——手撕队列习题
猜你喜欢
随机推荐
SQL review, grammar notes, fresh out
What is socket programming?
Redis advanced
838 · 子数组和为K
Define an abstract role class with name, age, gender, hobbies and other member variables. It is required to hide all variables as far as possible (private if possible), and then read and write each va
Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss
Design a circle class with private member radius representing radius and function get_ Radius () is used to obtain the radius and area () is used to calculate the area of the circle; (2) Define a tabl
Precautions for using feign to make remote calls
The only storage area in the JVM where GC and oom will not occur
Vivado generates and invokes EDF netlist files
L2-001 紧急救援 (最短路Dijkstra的扩展 - 最短路径数&路径最大权值)
867 · 四键键盘
1242 · 无重叠区间
The problem of interval summation -- difference
Detailed tree array template -- Theory and code implementation
1420 · 最小覆盖子串II
【题解】洛谷P6186 [NOI Online #1 提高组] 冒泡排序:【冒泡排序】与【逆序对】问题
Codeforces Round #778
[number theory] prime number (2): prime number sieve method (Egyptian sieve, Euler sieve, interval sieve)
LeetCode - 6 - (字符串相乘、下一个更大元素<ⅠⅡⅢ>、k个一组翻转链表)







![Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss](/img/4e/34e2820ff8579007b20b33b27d8f1d.png)

