PAT (Advanced Level) Practise - 1097. Deduplication on a Linked List (25)
http://www.patest.cn/contests/pat-a-practise/1097
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
此题是2015年春季的研究生入学考试复试时的机试题,链接 http://www.patest.cn/contests/graduate-entrance-exam-2015-03-20
1. 遇到-1就说明链表结束了,剩余未用的节点无法使用,是断链部分。
2. hash判重真好使。
3.输出格式略麻烦。
#include<iostream>
struct node
{
int data;
int next;
int flag;//标记 1绝对值首次出现,有效 2重复,无效 0不在链表上
}nodeinfo[]; int absolute[]={},value0=;
int headtrue=-,headfalse=-;
int numtrue=-,numfalse=-; int main()
{
for(int i=;i<;i++) absolute[i]=;
for(int i=;i<;i++) nodeinfo[i].next=-,nodeinfo[i].flag=; int n,address=,p=;
scanf("%d%d",&p,&n);
for(int i=;i<n;i++)
{
scanf("%d",&address);
scanf("%d%d",&nodeinfo[address].data,&nodeinfo[address].next);
} if(p==-) return ;
else if(n==) { printf("%05d %d -1",p,nodeinfo[p].data);return ;} while(p>=)
{
value0=nodeinfo[p].data;
if(value0<=) value0=-value0; //fabs(temp)
if(absolute[value0]) //绝对值已出现过
{
if(headfalse==-) headfalse=p,numfalse=;
nodeinfo[p].flag=,numfalse++;
}
else
{
if(headtrue==-) headtrue=p,numtrue=;
absolute[value0]=,nodeinfo[p].flag=,numtrue++;
} p=nodeinfo[p].next;
} int num=;
for(int k=;k<=;k++)
{
if(k==) p=headtrue,num=numtrue-;
else p=headfalse,num=numfalse-; for(int i=;i<=num;i++)
{ if(i) printf(" %05d\n",p);
if(i<num) printf("%05d %d",p,nodeinfo[p].data);
else printf("%05d %d -1\n",p,nodeinfo[p].data); p=nodeinfo[p].next;
while(i<num && nodeinfo[p].flag!=k)
p=nodeinfo[p].next;
}
} return ;
}
PAT (Advanced Level) Practise - 1097. Deduplication on a Linked List (25)的更多相关文章
- PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)
PAT (Basic Level) Practise (中文)-1025. 反转链表 (25) http://www.patest.cn/contests/pat-b-practise/1025 ...
- PAT (Basic Level) Practise (中文)-1030. 完美数列(25)
PAT (Basic Level) Practise (中文)-1030. 完美数列(25) http://www.patest.cn/contests/pat-b-practise/1030 给 ...
- PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642
PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642 题目描述: At the beginning of ever ...
- PAT (Advanced Level) 1097. Deduplication on a Linked List (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...
- PAT (Advanced Level) Practise 1004 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...
- PAT (Advanced Level) Practise - 1092. To Buy or Not to Buy (20)
http://www.patest.cn/contests/pat-a-practise/1092 Eva would like to make a string of beads with her ...
- PAT (Advanced Level) Practise - 1093. Count PAT's (25)
http://www.patest.cn/contests/pat-a-practise/1093 The string APPAPT contains two PAT's as substrings ...
- PAT (Advanced Level) Practise - 1095. Cars on Campus (30)
http://www.patest.cn/contests/pat-a-practise/1095 Zhejiang University has 6 campuses and a lot of ga ...
随机推荐
- HDU2087【KMP-next】
思路: 利用next[]的转化. //#include <bits/stdc++.h> #include<cstdio> #include<string.h> #i ...
- 解决overflow: auto在Ios中滑动不流畅
[bug]—— H5页面在 ios 端滑动不流畅的问题 IOS系统的惯性滑动效果非常6,但是当我们对div加overflow-y:auto;后是不会出这个效果的,滑动的时候会感觉很生涩.怎么办? ...
- cf786C(xjb)
题目链接:http://codeforces.com/problemset/problem/768/C 题意:给出一个数组,经过k次操作后最大元素和最小元素分别是什么.. 操作:给当前数组排序,再将第 ...
- 洛谷P2485 [SDOI2011]计算器(exgcd+BSGS)
传送门 一题更比三题强 1操作直接裸的快速幂 2操作用exgcd求出最小正整数解 3操作用BSGS硬上 然后没有然后了 //minamoto #include<cstdio> #inclu ...
- 使用JRegex抽取网页信息
当网络爬虫将网页下载到磁盘上以后,需要对这些网页中的内容进行抽取,为索引做准备.一个网页中的数据大部分是HTML标签,索引肯定不会去索引这些标签.也就是说,这种信息是没有用处的信息,需要在抽取过程中过 ...
- 使用jqzoom插件时
[javascript] view plaincopy /*使用jqzoom*/ $(function() { $(".jqzoom").jqueryzoom({ xzoom: 3 ...
- 最短路之SPFA(单源)HDU 2544
#include <iostream> #include <queue> #include <algorithm> #define MAXLEN 1005 #def ...
- Restful API官方文档
理解Restful架构:http://www.ruanyifeng.com/blog/2011/09/restful RESTful设计指南:http://www.ruanyifeng.com/blo ...
- UWP 版本号
一:版本号 个人开发者对于版本号的命名相对随便一点,如果是大公司的话,命名则要规范一点.以开发UWP为例 在创建包的时候,开发者可以自定义版本号或者点击自动增加. 二:对应上图中的四个格子中的数字 第 ...
- python基本的数据类型
一.python的基本数据类型 int => 整数,主要用来进行数学运算 str ==> 字符串 可以用来保存少量数据并进行相应操作 bool ==> 判断真假,True,False ...