Topic links
l2-002. Link list time limit
Memory Limit 65536 KB
Code length limit 8000 B
Chen, standard author of the procedure for the award of questions
Given a single-linked list L with integer key values, the subject asks you to write a program that deletes the absolute value of those key values with duplicate nodes. That is, for any key value K, only the first node with a key value or its absolute value equal to K can be preserved. At the same time, all the deleted nodes must be saved in another linked list. For example, if the other L is 21→-15→-15→-7→15, then you must output the linked list 21→-15→-7, and the deleted list -15→15.
Input Format:
Enter the first row containing the address of the first node of the list, and the number of nodes N (positive integer <= 105). The node address is a non-negative 5-bit integer, and the null pointer is represented by-1.
Next n rows, each row gives information about a node in the following format:
Address Key Next
Where address is the location of the node, key is an integer value not exceeding 104, and next is the address of the next node.
output Format:
First output the linked list after the deduplication, and then output the linked list of deleted nodes. Each node occupies one row and is output in the format entered. Input Sample:
00100 5
99999-7 87654
23854-15 00000
87654 15-1
00000-15 99999
00100 21 23854
Sample output:
00100 23854
23854-15 99999
99999-7-1
00000-15 87654
87654 15-1
The puzzle: Defines two structures, stores the original linked list and the output of the linked list.
Defines an array initialized to zero to mark the occurrence of a key and, if repeated, to connect it to the list of outputs.
#include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <vector
> #include <math.h> #include <string.h> #include <string> #include <map> using namespace std;
typedef long Long LL; struct node{int key,next;}
A[100005];
int b[100005];
Node c[100005];
int main () {int s,n;
cin>>s>>n;
int ad;
for (int i=1;i<=n;i++) {cin>>ad;
cin>>a[ad].key>>a[ad].next;
} int now=-1,flag=0,st;
C[now].next=-1;
for (int i=s;~i;) {printf ("%05d%d", i,a[i].key);
B[abs (A[i].key)]=1;
I=a[i].next;
while (I>=0&&b[abs (A[i].key))) {if (!flag) {now=st=i;
C[now].key=a[i].key;
} else{c[now].next=i;
C[i].key=a[i].key;
C[i].next=-1;
Now=i;
} I=a[i].next; flag=1;
} if (i>=0) printf ("%05d\n", I);
else printf (" -1\n");
} if (flag) for (int i=st;~i;i=c[i].next) {printf ("%05d%d", i,c[i].key);
if (c[i].next>=0) printf ("%05d\n", C[i].next);
else printf (" -1\n");
}
}