PAT A 1097. Deduplication on a Linked List (25)
来源:互联网 发布:压力容器设计 知乎 编辑:程序博客网 时间:2024/06/03 14:26
题目
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, andNext 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 23854
Sample Output:
00100 21 2385423854 -15 9999999999 -7 -100000 -15 8765487654 15 -1
简单地统计一下,输出即可。
#include <iostream>#include <algorithm>#include <cmath>#include <vector>using namespace std;struct node//节点{int address,value,next;};int main(){int head,n;cin>>head>>n;vector<node> data(100000);//表示相应内存中的点node nd;for(int i=0;i<n;i++)//输入{scanf("%d %d %d",&nd.address,&nd.value,&nd.next);data[nd.address]=nd;}vector<int> count(10000,0);//相应绝对数的key出现标记vector<node> list1,list2;//两个链int pos=head;while(pos!=-1){if(count[abs(data[pos].value)]==0)//第一次,放入链1list1.push_back(data[pos]);else//否则,放入链2list2.push_back(data[pos]);count[abs(data[pos].value)]=1;pos=data[pos].next;}for(int i=0;i<list1.size();i++)//输出{if(i+1<list1.size())printf("%05d %d %05d\n",list1[i].address,list1[i].value,list1[i+1].address);elseprintf("%05d %d -1\n",list1[i].address,list1[i].value);}for(int i=0;i<list2.size();i++){if(i+1<list2.size())printf("%05d %d %05d\n",list2[i].address,list2[i].value,list2[i+1].address);elseprintf("%05d %d -1\n",list2[i].address,list2[i].value);}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)
- Linux笔记(一)-了解Linux
- php中出现Notice: Uninitialized string offset: 0原因及解决办法
- android 电话拨号器
- [Leetcode] 94. Binary Tree Inorder Traversal
- Linux下搭建PHP开发环境(Apache2.4.12+PHP5.6.7+MySQL5.6)
- PAT A 1097. Deduplication on a Linked List (25)
- Android 使用NineOldAndroids实现绚丽的ListView左右滑动删除Item效果
- Win下Linux虚拟机登陆后屏幕没有菜单的解决方法
- 为什么list最好=new arraylist
- 程序员之路
- MMU、Cache、TLB、流水线
- 一个JDK1.5的新特性:枚举学习笔记
- 2015年 4月2号的日志
- java序列化