PAT 1097.Deduplication on a Linked List

1097. Deduplication on a Linked List (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 #include<stdio.h>
 2 #include<vector>
 3 using namespace std;
 4 
 5 class Node{
 6 public:
 7     int key;
 8     int add;
 9     int next;
10 };
11 Node node[100000];
12 bool value[10005];
13 
14 int main(void){
15     int root, n, temp;
16     int i;
17     int add, key, next;
18     vector<Node> list, left;
19     
20     scanf("%d %d", &root, &n);
21     for (i = 0; i < n; i++){
22         scanf("%d %d %d", &add, &key, &next);
23         node[add].add = add;
24         node[add].key = key;
25         node[add].next = next;
26     }
27     for (temp = root; temp != -1; temp = node[temp].next){
28         key = node[temp].key;
29         if (key < 0)
30             key = -key;
31         if (!value[key]){
32             list.push_back(node[temp]);
33             value[key] = true;
34         }
35         else{
36             left.push_back(node[temp]);
37         }
38     }
39     for (i = 0; i < list.size(); i++){
40         printf("%05d %d", list[i].add, list[i].key);
41         if (i != list.size() - 1){
42             printf(" %05d", list[i + 1].add);
43         }
44         else{
45             printf(" -1");
46         }
47         printf("\n");
48     }
49     for (i = 0; i < left.size(); i++){
50         printf("%05d %d", left[i].add, left[i].key);
51         if (i != left.size() - 1){
52             printf(" %05d", left[i + 1].add);
53         }
54         else{
55             printf(" -1");
56         }
57         printf("\n");
58     }
59 
60     return 0;
61 }

 

posted @ 2018-02-04 15:47  GrayWind  阅读(130)  评论(0)    收藏  举报