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)的更多相关文章

  1. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

    PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025 ...

  2. PAT (Basic Level) Practise (中文)-1030. 完美数列(25)

    PAT (Basic Level) Practise (中文)-1030. 完美数列(25)   http://www.patest.cn/contests/pat-b-practise/1030 给 ...

  3. 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 ...

  4. PAT (Advanced Level) 1097. Deduplication on a Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  5. 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 ...

  6. PAT (Advanced Level) Practise 1004 解题报告

    GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Git之常用的命令操作

    set LESSCHARSET=utf-8 git设置文件名大小写敏感 git branch -rgit checkout 'TestMaster'git checkout -b 'TestMaste ...

  2. UIWebView与JavaScript的交互

    UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS ...

  3. Java - 安装jdk并设置环境变量

    前言 双十一买了台新的笔记本,需要重新安装下Java,这里记录下安装的过程,毕竟万事开头难,就算是老手也不一定能一次就把Java安装成功. 安装jdk 作为一名Java开发,当然是要安装jdk了,如果 ...

  4. 相册选择头像或者拍照 上传头像以NSData 图片二进制格式 表单上传

    一.点击头像图片 或者按钮 在相册选择照片返回img,网络上传头像要用data表单上传 (1)上传头像属性 // 图片二进制格式 表单上传 @property (nonatomic, strong) ...

  5. 手动释放Linux内存

    查看内存: [root@iZ9dp52tlpqyihuisujjswZ bin]# free -h total used free shared buff/cache available Mem: 3 ...

  6. morphia(1)-基础

    二.Mapping classes entity类上加注解:@Entity,其成员变量必须有@Id @Id private ObjectId id; 其在mongodb中变量名: _id @Embed ...

  7. 转 Linux SendMail发送邮件失败诊断案例(四)

    http://www.cnblogs.com/kerrycode/p/7826036.html

  8. Spring的ioc(DI)复习概念和原理简介

    IOC的好处 ioc或者说di的概念很显然了,反转控制和依赖注入,那本来直接new就行的东西,为什么要搞这么复杂呢?? 开发维护方便,高层设计不用依赖底层的,不然底层一个类改下构造器,高层就全要改,因 ...

  9. Sql Server的两个小技巧

    创建表结构 CREATE TABLE test( ,) NOT NULL PRIMARY KEY, ) COLLATE Chinese_PRC_CI_AS NULL, createdTime DATE ...

  10. bootstrap-table 基础用法

    1.需要添加的引用. <script src="@Url.Content("~/js/jquery-2.1.1.js")"></script& ...