1097
来源:互联网 发布:javascript 设置css 编辑:程序博客网 时间:2024/06/10 02:03
1097. Deduplication on a Linked List (25)
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 23854Sample Output:
00100 21 2385423854 -15 9999999999 -7 -100000 -15 8765487654 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[N];// 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 main(void){ int startA, n; scanf("%d%d", &startA, &n); int add; //input node for (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 link int 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 -1 if (endA1 != -1) node[endA1].next = -1; if (endA2 != -1) node[endA2].next = -1; printLink(startA1); printLink(startA2); return 0;}
- 1097
- 1097
- problem 1097
- ZJU-1097
- hdoj 1097
- HD 1097
- hdu 1097
- hdu 1097
- hdu 1097
- hdu 1097
- HDU 1097
- ZOJ-1097
- hdu 1097
- PAT 1097
- hdu 1097
- pat 1097
- csust 1097
- 1097 最小公倍数
- XML 特殊字符处理
- TP框架中RBAC的介绍与认识--适合菜鸟初学者
- android.os.NetworkOnMainThreadException 解决办法
- NYOJ 26 最长公共子序列
- HDU 5289-B - Assignment-贪心
- 1097
- 操作系统---精髓与设计原理 之 计算机系统概述
- 读写Excel数据
- POJ 2253 Frogger
- 字典树模版
- 生活在这个看脸看看身材的时代、莫做黄脸婆
- HDU 5083 Instruction(大模拟)
- 如何读写二进制文件
- android apk文件安装