PAT 1097. Deduplication on a Linked List (25)(链表问题)(链表分段)
2016-10-29 13:28
519 查看
官网
题目
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 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
解题思路
1.题目意思是给你一个链表,把链表中值第一次出现的那些链表保存下来,不是第一次出现的统一放在一起,最后分别输出上述俩部分。AC代码
#include <iostream> #include <math.h> #include<map> #include<vector> using namespace std; map<int, int> value, nxt; int first, n; int key[10001]; vector<int> keep, other; int main() { cin >> first >> n; int id, val, nx; for (int i = 0; i < n; i++) { cin >> id >> val >> nx; value[id] = val; nxt[id] = nx; } int now = first; while (now!=-1) { //如果第一出现就存到keep,否则存到other; if (key[abs(value[now])]==0) { keep.push_back(now); key[abs(value[now])] = 1; } else { other.push_back(now); } now = nxt[now]; } //分别输出 for (int i = 0; i < (int)keep.size(); i++) { printf("%05d %d", keep[i], value[keep[i]]); if (i==(int)keep.size()-1) { printf(" -1\n"); } else{ printf(" %05d\n", keep[i + 1]); } } for (int i = 0; i < (int)other.size(); i++) { printf("%05d %d", other[i], value[other[i]]); if (i == (int)other.size() - 1) { printf(" -1\n"); } else{ printf(" %05d\n", other[i + 1]); } } return 0; }
相关文章推荐
- PAT 1133. Splitting A Linked List (25) 链表的拆分和合并
- pat甲级1074. Reversing Linked List (25)、乙级1025. 反转链表 (25)
- 1025. 反转链表 (25)PAT乙级&&1074. Reversing Linked List (25)PAT甲级
- PAT (Advanced Level) 1097. Deduplication on a Linked List (25) 链表去重
- 02-1 Reversing Linked List (PAT) - 链表问题
- PAT (Advanced Level) 1074. Reversing Linked List (25) 翻转链表
- PAT - 甲级 - 1074. Reversing Linked List (25)(链表)
- PAT 1097. Deduplication on a Linked List (25) 剔除链表中的重复节点,set用法,尾插法
- PAT甲题题解-1074. Reversing Linked List (25)-求反向链表
- PAT - 甲级 - 1097. Deduplication on a Linked List (25)(链表)
- PAT 1041 Linked List Sorting (25)(链表排序)
- PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作
- 1074. Reversing Linked List (25)【链表翻转】——PAT (Advanced Level) Practise
- PAT 1074. Reversing Linked List (25)(链表反转)
- PAT (Advanced Level) Practise - 1097. Deduplication on a Linked List (25)
- 【PAT】1097. Deduplication on a Linked List (25)
- PAT 1097. Deduplication on a Linked List (25)
- c# 链表(LinkedList)使用中的问题(.net framework 2.0)
- PAT 1097. Deduplication on a Linked List (25)
- 链表的反转问题(Reverse Linked List)