PAT 1097-Deduplication on a Linked List (25)
来源:互联网 发布:金蝶软件数据库安装 编辑:程序博客网 时间:2024/06/15 04:53
题目描述
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.
输入描述:
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.
输出描述:
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.
输入例子:
00100 599999 -7 8765423854 -15 0000087654 15 -100000 -15 9999900100 21 23854
输出例子:
00100 21 2385423854 -15 9999999999 -7 -100000 -15 8765487654 15 -1
解题思路
该题非常简单,普通的链表题,读懂题目即可。由于本人很讨厌开很大的内存和固定大小数组,结果在这道题上吃了亏,时间超时。所以在做PAT的测试题时,开固定数组是没什么问题的,一般都在要求内存内。
#include<iostream>#include<stdio.h>#include<vector>using namespace std;struct node{int address;int key;int next;};int main(){int N,start,temp;vector<node> nodes(100000);vector<int> reserve, remove;vector<int> key(10001, 0);cin >> start >> N;for (int i = 0; i < N; i++){cin >> temp;cin >> nodes[temp].key >> nodes[temp].next;nodes[temp].address = temp;}temp = start;for (int i = 0; i < N; i++){if (key[abs(nodes[temp].key)]){if (!remove.empty())nodes[remove.back()].next = nodes[temp].address;remove.push_back(temp);}else{if (!reserve.empty())nodes[reserve.back()].next = nodes[temp].address;reserve.push_back(temp);key[abs(nodes[temp].key)] = 1;}temp = nodes[temp].next;}for (int i = 0; i < reserve.size() - 1; i++)printf("%05d %d %05d\n", nodes[reserve[i]].address, nodes[reserve[i]].key, nodes[reserve[i]].next);printf("%05d %d -1\n", nodes[reserve.back()].address, nodes[reserve.back()].key);for (int i = 0; i < remove.size() - 1; i++)printf("%05d %d %05d\n", nodes[remove[i]].address, nodes[remove[i]].key, nodes[remove[i]].next);printf("%05d %d -1\n", nodes[remove.back()].address, nodes[remove.back()].key);return 0;}
0 0
- PAT 1097 Deduplication on a Linked List
- pat 1097 Deduplication on a Linked List
- PAT 1097 Deduplication on a Linked List
- PAT-Deduplication on a Linked List (25)
- PAT 1097 Deduplication on a Linked List (25)
- PAT 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 A 1097. Deduplication on a Linked List (25)
- PAT--1097. Deduplication on a Linked List
- 【PAT】1097. Deduplication on a Linked List
- PAT-Deduplication on a Linked List
- 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甲级练习题A1097. Deduplication on a Linked List (25)
- PAT 1097. Deduplication on a Linked List (25)
- 什么时候用activity什么时候用fragment
- ssm项目中的文件上传
- 【VHDL】基本组合电路的设计
- ZCMU—1889
- hdu 2030 汉字统计
- PAT 1097-Deduplication on a Linked List (25)
- 嵌套类、局部类
- [BZOJ3531] SDOI2014 树链剖分+动态开点线段树
- BroadCastReceiver无法接收到广播的一种可能
- 【Codeforces-402B】-Weird Rounding(数学,思维)
- 初学集合框架的几点总结
- 海量数据处理问题之Bit-Map详解
- 黑车司机发车神器(JAVA)
- POJ 3196 Babylonian Roulette G++