1097. Deduplication on a Linked List (25) time limit MS Memory limit 65536 KB code length limit 16000 B procedure StandardAuthor Chen, Yue
Given a singly linked list L with integer keys, you is supposed to remove the nodes with duplicated absolute values of th e keys. That's, for each value K, only the first node of which the value or absolute value of its key equals K would be kept. At the mean time, all the removed nodes must is kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list-15→15.
Input Specification:
Each input file contains the one test case. The first line contains the address of the first node, and a positive N (<=) which are the total numb Er of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by-1.
Then N lines follow, each describes a node in the format:
Address Key Next
Where Address is the position of the node, Key is a integer of which absolute value is no more than 104 , and next is the position of the next node.
Output Specification:
For each case, output the resulting linked list first and then the removed list. Each node occupies a line, and was printed in the same format as in the input.
Sample Input:
00100 599999-7 8765423854-15 0000087654 15-100000-15 9999900100 21 23854
Sample Output:
00100 21 2385423854-15 9999999999-7-100000-15 8765487654 15-1
Submit Code
For convenience, add a head pointer.
1#include <cstdio>2#include <stack>3#include <algorithm>4#include <iostream>5#include <stack>6#include <Set>7#include <map>8 using namespacestd;9 structndoe{Ten intK,next; One }; ANdoe mem[100005]; - BOOLha[10005]; - intAbsinta) { the if(a<0){ -a=-A; - } - returnA; + } - #defineRefirst 100001 + #defineNrfirst 100002 A intMain () { at //freopen ("D:\\input.txt", "R", stdin); - intN,first; -scanf"%d%d",&first,&n); - inti,cur; - for(i=0; i<n;i++){ -scanf"%d",&cur); inscanf"%d%d",&mem[cur].k,&mem[cur].next); - } toCur=Nrfirst; + intrelist=refirst,p=First ; - while(p!=-1){ the while(p!=-1&&Ha[abs (MEM[P].K)]) { *mem[relist].next=p; $relist=p;Panax Notoginsengp=Mem[p].next; - } the if(p!=-1){ +Ha[abs (MEM[P].K)]=true; A } themem[cur].next=p; + if(p!=-1){ -Cur=p; $p=Mem[cur].next; $ } - } -mem[relist].next=-1; thep=Mem[nrfirst].next; - while(p!=-1){Wuyi if(mem[p].next!=-1){ theprintf"%05d%d%05d", p,mem[p].k,mem[p].next); - } Wu Else{ -printf"%05d%d%d", p,mem[p].k,mem[p].next); About } $printf"\ n"); -p=Mem[p].next; - } -p=Mem[refirst].next; A while(p!=-1){ + if(mem[p].next!=-1){ theprintf"%05d%d%05d", p,mem[p].k,mem[p].next); - } $ Else{ theprintf"%05d%d%d", p,mem[p].k,mem[p].next); the } theprintf"\ n"); thep=Mem[p].next; - } in return 0; the}
pat1097. Deduplication on a Linked List