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 23854Sample 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;}
相关文章推荐
- [C/C++]反转链表
- C#实现基于链表的内存记事本实例
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- C++实现简单的学生管理系统
- Linux内核链表实现过程
- C++链表倒序实现方法
- C#通过链表实现队列的方法
- 找出链表倒数第n个节点元素的二个方法
- Java数据结构之简单链表的定义与实现方法示例
- C语言单循环链表的表示与实现实例详解
- C++实现的链表类实例
- PHP小教程之实现链表
- C语言双向链表的表示与实现实例详解
- C语言实现输出链表中倒数第k个节点
- C++语言实现线性表之链表实例
- STL list链表的用法详细解析
- C语言创建链表错误之通过指针参数申请动态内存实例分析
- php链表用法实例分析
- JavaScript中数据结构与算法(三):链表
- 编码实现从无序链表中移除重复项(C和JAVA实例)