Deduplication意为重复数据消除,Deplicated意为复制,是deduplication的过去式,题干大意为给定一个单链表L要求你对每一个结点的值判断,如果这个值的绝对值是第一次出现,那么将它们放在一起,如果这个结点的绝对值已经出现过了,那么按照出现顺序将他们单独存放,最后按照链表的形式输出,先输出第一次出现的主表,然后在输出重复的链表,这里题目有一个坑点,就是有一种3分的情况是没有重复的值得链表,这样的话需要判断再输出,具体方法PAT已经出过好多这种类似的题目了,无非就是对节点地址去重然后根据要求判断最后控制双重key标记的语句输出即可
代码:
#include<iostream>
#include<stdio.h>
#include<vector>
#include<unordered_map>
using namespace std;
const int maxn = 1e5+10;
struct node{
int data;
int next;
}list[maxn];
int main() {
int first, n;
cin >> first >> n;
while (n--) {
int temp;
cin >> temp;
cin >> list[temp].data >> list[temp].next;
}
vector<int> vt;
while (first != -1) {
vt.push_back(first);
first = list[first].next;
}
unordered_map<int, int> mp;
vector<int> ans[2];
for (auto it : vt) {
if (mp[abs(list[it].data)] == 0) {
ans[0].push_back(it);
mp[abs(list[it].data)] = 1;
} else {
ans[1].push_back(it);
}
}
int key = 0;
for (auto it : ans[0]) {
if (!key) {
printf("%05d %d ", it, list[it].data);
key = 1;
} else {
printf("%05d\n%05d %d ", it, it, list[it].data);
}
}
cout << -1 << endl;
key = 0;
for (auto it : ans[1]) {
if (!key) {
printf("%05d %d ", it, list[it].data);
key = 1;
} else {
printf("%05d\n%05d %d ", it, it, list[it].data);
}
}
if (key)
cout << -1 << endl;
return 0;
}