Linked list deduplication : https://pintia.cn/problem-sets/1497870867886116864/problems/1497870914212204608
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
typedef struct node{
string addr;
int val;
string next;
}node;
node save[100010],del[100010];
map<string,node>mapt;
map<int,bool> ck;
int main(){
int n,cnt1=0,cnt2=0;
string st;
cin>>st>>n;
string a,b;int e;
for(int i = 0; i <n;i++){
cin>>a>>e>>b;
mapt[a] = {
a,e,b}; //这就是为什么要用map的原因了
}
while(st!="-1"){
if(ck[mapt[st].val]){
del[cnt1++] = mapt[st];
st = mapt[st].next;
continue;
}
ck[mapt[st].val] = true;
ck[-mapt[st].val] = true;
save[cnt2++] = mapt[st];
st = mapt[st].next;
}
for(int i = 0; i <cnt2;i++){
if(save[i].next!=save[i+1].addr) save[i].next = save[i+1].addr;
if(i==cnt2-1) save[i].next = "-1";
}
for(int i = 0; i < cnt1;i++){
if(del[i].next!=del[i+1].addr) del[i].next = del[i+1].addr;
if(i==cnt1-1) del[i].next = "-1";
}
for(int i= 0; i < cnt2;i++) cout<<save[i].addr<<" "<<save[i].val<<" "<<save[i].next<<endl;
for(int i= 0; i <cnt1;i++) cout<<del[i].addr<<" "<<del[i].val<<" "<<del[i].next<<endl;
return 0;
}
Then
Just ask Brother Tao and add a word.
#define endl '\n'
Final code:
#include<bits/stdc++.h>
#include<iostream>
#define endl '\n'
using namespace std;
typedef struct node{
string addr;
int val;
string next;
}node;
node save[100010],del[100010];
map<string,node>mapt;
map<int,bool> ck;
int main(){
int n,cnt1=0,cnt2=0;
string st;
cin>>st>>n;
string a,b;int e;
for(int i = 0; i <n;i++){
cin>>a>>e>>b;
mapt[a] = {
a,e,b}; //这就是为什么要用map的原因了
}
while(st!="-1"){
if(ck[mapt[st].val]){
del[cnt1++] = mapt[st];
st = mapt[st].next;
continue;
}
ck[mapt[st].val] = true;
ck[-mapt[st].val] = true;
save[cnt2++] = mapt[st];
st = mapt[st].next;
}
for(int i = 0; i <cnt2;i++){
if(save[i].next!=save[i+1].addr) save[i].next = save[i+1].addr;
if(i==cnt2-1) save[i].next = "-1";
}
for(int i = 0; i < cnt1;i++){
if(del[i].next!=del[i+1].addr) del[i].next = del[i+1].addr;
if(i==cnt1-1) del[i].next = "-1";
}
for(int i= 0; i < cnt2;i++) cout<<save[i].addr<<" "<<save[i].val<<" "<<save[i].next<<endl;
for(int i= 0; i <cnt1;i++) cout<<del[i].addr<<" "<<del[i].val<<" "<<del[i].next<<endl;
return 0;
}
This senior from Station B talks very well: https://www.bilibili.com/video/BV1Ka411t7Bj?spm_id_from=333.999.0.0