PAT A1097

Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be 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 one test case. For each case, the first line contains the address of the first node, and a positive N (<= 105) which is the total number 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 an 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, then the removed list. Each node occupies a line, and is printed in the same format as in the input.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "stdio.h"
//#include "math.h"
#include "string.h"
//#include "iostream"
#include "stdlib.h"
#include "vector"
//#include "set"
//#include "map"
//#include "stack"
//#include "queue"
#include "algorithm"
using namespace std;

//typedef long long LL;
const int maxn = 100005;
const int TABLE = 100010;
struct NODE{
int address, data, next;
}node[maxn];
bool isExist[TABLE] = {false};//绝对值是否已经出现
vector<NODE> list1,list2;//没被删除和被删除
int main(){
fill(isExist, isExist + TABLE, false);//初始化isExist为未出现
// memset(isExist, false, sizeof(isExist));
int n, begin, address;
scanf("%d%d", &begin, &n);
for (int i = 0; i < n; i++) {
scanf("%d",&address);
scanf("%d%d", &node[address].data, &node[address].next);
node[address].address = address;
}

for (int p = begin; p != -1; p = node[p].next) {
if (isExist[abs(node[p].data)] == false) {//不存在
isExist[abs(node[p].data)] = true;
list1.push_back(node[p]);
}else{
list2.push_back(node[p]);
}
}
//输出未删除的
for (int i = 0; i < list1.size(); i++) {
if (i < list1.size() - 1) {
printf("%05d %d %05d\n", list1[i].address, list1[i].data, list1[i+1].address);
}else{
printf("%05d %d -1\n", list1[i].address, list1[i].data);
}
}
//输出被删除的
for (int i = 0; i < list2.size(); i++) {
if (i < list2.size() - 1) {
printf("%05d %d %05d\n", list2[i].address, list2[i].data, list2[i+1].address);
}else{
printf("%05d %d -1\n", list2[i].address, list2[i].data);
}
}
return 0;
}

书上答案,思路巧妙,用每个结点加一个order,用maxn,2*maxn将静态链表分为 未删除的结点,删除的结点,无效结点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "stdio.h"
//#include "math.h"
#include "string.h"
//#include "iostream"
#include "stdlib.h"
#include "vector"
//#include "set"
//#include "map"
//#include "stack"
//#include "queue"
#include "algorithm"
using namespace std;

//typedef long long LL;
const int maxn = 100005;
const int TABLE = 100010;
struct NODE{
int address, data, next;
int order;
}node[maxn];
bool isExist[TABLE] = {false};//绝对值是否已经出现
bool cmp(NODE a, NODE b){
return a.order < b.order;
}
int main(){
fill(isExist, isExist + TABLE, false);//初始化isExist为未出现
// memset(isExist, false, sizeof(isExist));
for (int i = 0; i < maxn; i++) {
node[i].order = 2 * maxn;
}
int n, begin, address;
scanf("%d%d", &begin, &n);
for (int i = 0; i < n; i++) {
scanf("%d",&address);
scanf("%d%d", &node[address].data, &node[address].next);
node[address].address = address;
}
int countValid = 0, countRemoved = 0, p = begin;
while (p != -1){
if (isExist[abs(node[p].data)] == false) {//data绝对值不存在
isExist[abs(node[p].data)] = true;
node[p].order = countValid++;
}else{
node[p].order = maxn + countRemoved++;
}
p = node[p].next;
}

sort(node, node+maxn, cmp);
int count = countValid + countRemoved;
for (int i = 0; i < count; i++) {
if (i != countValid -1 && i != count -1) {
printf("%05d %d %05d\n", node[i].address, node[i].data, node[i + 1].address);
}else{
printf("%05d %d -1\n", node[i].address, node[i].data);
}
}
return 0;
}