您的位置:首页 > 其它

1097

2015-08-15 09:15 337 查看

1097. Deduplication on a Linked List (25)

时间限制300 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueGiven 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 meantime, 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 Nextwhere 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
#include <stdio.h>#include <iostream>#include <cstring>#include <cmath>using namespace std;struct Node{int value;int next;};// M add, N valueconst int M = 100001;const int N = 10001;Node node[M];bool isexit;// divide two links, print link nodevoid printLink(int add){while (add != -1){printf("%05d %d ", add, node[add].value);add = node[add].next;add == -1 ? printf("-1\n"):printf("%05d\n", add);}}int m4000ain(void){int startA, n;scanf("%d%d", &startA, &n);int add;//input nodefor (int i = 0; i < n; ++i){scanf("%d", &add);scanf("%d%d", &node[add].value, &node[add].next);}memset(isexit, false, sizeof(isexit));// divide two linkint startA1 = -1, endA1 = -1, startA2 = -1, endA2 = -1;add = startA;while (add != -1){if (!isexit[abs(node[add].value)]){if (startA1 == -1){startA1 = endA1 = add;}else{node[endA1].next = add;endA1 = add;}isexit[abs(node[add].value)] = true;}else{if (startA2 == -1){startA2 = endA2 = add;}else{node[endA2].next = add;endA2 = add;}}add = node[add].next;}// set the tail -1if (endA1 != -1)node[endA1].next = -1;if (endA2 != -1)node[endA2].next = -1;printLink(startA1);printLink(startA2);return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  链表