1097. Deduplication on a Linked List (25)

来源:互联网 发布:李贞贤阿里阿里百度云 编辑:程序博客网 时间:2024/06/05 11:40

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 599999 -7 8765423854 -15 0000087654 15 -100000 -15 9999900100 21 23854
Sample Output:
00100 21 2385423854 -15 9999999999 -7 -100000 -15 8765487654 15 -1

 

#include <stdio.h>  #include <stdlib.h>  #include <math.h>#define MAX 100010typedef struct node{int key;int next;}NODE;NODE input[MAX];int output[MAX], OutputIndex;int removed[MAX], RemoveIndex;int FlagList[MAX];int main(){int StartAddress, N, i;int address, key, next;//freopen("d:\\input.txt", "r", stdin);scanf("%d%d", &StartAddress, &N);for (i = 0; i < N; i++){scanf("%d%d%d", &address, &key, &next);input[address].key = key;input[address].next = next;}address = StartAddress;while (address != -1){if (FlagList[abs(input[address].key)] == 0){FlagList[abs(input[address].key)]++;output[OutputIndex++] = address;}else{removed[RemoveIndex++] = address;}address = input[address].next;}for (i = 0; i < OutputIndex - 1; i++){printf("%05d %d %05d\n", output[i], input[output[i]].key, output[i + 1]);}if (i < OutputIndex){printf("%05d %d -1\n", output[i], input[output[i]].key);}for (i = 0; i < RemoveIndex - 1; i++){printf("%05d %d %05d\n", removed[i], input[removed[i]].key, removed[i + 1]);}if (i < RemoveIndex){printf("%05d %d -1\n", removed[i], input[removed[i]].key);}return 0;}


 

 

 

0 0
原创粉丝点击