PAT-A-1097. Deduplication on a Linked List (25)
来源:互联网 发布:qq飞车风火轮数据 编辑:程序博客网 时间:2024/06/10 22:08
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<iostream>#include<cstdio>#include<stdio.h>#include<algorithm>#include<math.h>#include<cstring>using namespace std;const int maxn = 100010;const int num = 100100;struct Node{ int addr; int date; int next; int order;}node[maxn];bool flag[num] = { false };int cmp(Node a, Node b){ return a.order < b.order;}int main(){ memset(flag, false, sizeof(flag)); for (int i = 0; i < maxn; i++) node[i].order = 2 * maxn; int temp, n, begin; cin >> begin >> n; for (int i = 0; i < n; i++) { cin >> temp; cin >> node[temp].date >> node[temp].next; node[temp].addr = temp; } int ansnum = 0; int deletenum = 0; for (int p = begin; p != -1; p = node[p].next) { if (flag[abs(node[p].date)] == false) { flag[abs(node[p].date)] = true; node[p].order = ansnum++; } else { node[p].order = maxn + deletenum++; } } sort(node, node + maxn, cmp); for (int i = 0; i < ansnum; i++) { if (i != ansnum - 1) printf("%05d %d %05d\n", node[i].addr, node[i].date, node[i + 1].addr); else printf("%05d %d -1\n", node[i].addr, node[i].date); } for (int i = ansnum; i < ansnum + deletenum; i++) { if (i != ansnum + deletenum - 1) printf("%05d %d %05d\n", node[i].addr, node[i].date, node[i + 1].addr); else printf("%05d %d -1\n", node[i].addr, node[i].date); } system("pause"); return 0;}
- PAT--1097. Deduplication on a Linked List
- 【PAT】1097. Deduplication on a Linked List
- PAT-Deduplication on a Linked List (25)
- PAT A 1097. Deduplication on a Linked List (25)
- PAT-A-1097. Deduplication on a Linked List (25)
- PAT A 1097. Deduplication on a Linked List (25)
- PAT 1097. Deduplication on a Linked List (25)
- [PAT]1097. Deduplication on a Linked List (25)
- 【PAT】1097. Deduplication on a Linked List (25)
- PAT 1097. Deduplication on a Linked List (25)
- PAT 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25) PAT甲级
- PAT甲级练习1097. Deduplication on a Linked List (25)
- 【PAT】【Advanced Level】1097. Deduplication on a Linked List (25)
- PAT 甲级 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25)
- MAC允许任何来源应用运行
- mysql查看执行计划
- JavaScript prototype
- Android数据存储——文件ANDsharepreferences
- 试题测试(错误)
- PAT-A-1097. Deduplication on a Linked List (25)
- Go语言的并发和并行
- java calendar类的引用
- unity 接入安卓微信sdk之打jar包给unity使用
- android-ndk-r10e开启C++11,编译TEMP_FAILURE_RETRY错误
- 【linux】后台及进程相关
- Oracle基本操作十四:触发器-重点是DML行级触发器
- 【设计模式】——抽象工厂Abstract Factory
- Java动态代理