A1097 Deduplication on a Linked List (25point(s))
题意
绝对值相同的节点另成一条链表
思路
按照题意写即可。
总结
简单链表操作题。
Sample Input:
00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854
Sample Output:
00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1
#include"bits/stdc++.h"
using namespace std;
struct Node {
int v;
int nextAddr = -1;
};
const int maxn = 100000;
Node nodes[maxn];
void printList(vector<int>& a) {
for(int i=0; i<a.size(); i++) {
int addr = a[i];
int v = nodes[addr].v;
if(i == a.size()-1) {
printf("%05d %d -1\n",addr,v);
}else
printf("%05d %d %05d\n",addr,v,a[i+1]);
}
}
int main() {
//freopen("input.txt","r",stdin);
int headAddr ,n;
cin >> headAddr >> n;
for(int i=0; i<n; i++) {
int addr;
scanf("%d",&addr);
scanf("%d%d",&nodes[addr].v,&nodes[addr].nextAddr);
}
map<int,int> cnt; //key unique
int id = headAddr;
vector<int> ans,ans_remove;
while(id != -1) {
if(!cnt[abs(nodes[id].v)]) {
ans.push_back(id);
} else
ans_remove.push_back(id);
cnt[abs(nodes[id].v)] = 1;
id = nodes[id].nextAddr;
}
printList(ans);
printList(ans_remove);
}