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. HDU3478 【判奇环/二分图的性质】

    题意: 给你一幅图,给你一个起点,然后问你存不存在一个时刻,所有点可以在那个时刻到达. 思路: 这幅图首先是联通的: 如果出现奇数环,则满足在某一时刻都可能到达: 然后判断奇数环用二分图性质搞也是神奇 ...

  2. bzoj 4200: [Noi2015]小园丁与老司机【dp+有上下界最小流】

    洛谷上有个点死活卡不过去,不知道是哪里写丑了orz 参考:https://www.cnblogs.com/ditoly/p/BZOJ4200.html 从上往下dp,设f为不向左右走直接上去的值,g为 ...

  3. 洛谷P2824 [HEOI2016/TJOI2016]排序(线段树)

    传送门 这题的思路好清奇 因为只有一次查询,我们考虑二分这个值为多少 将原序列转化为一个$01$序列,如果原序列上的值大于$mid$则为$1$否则为$0$ 那么排序就可以用线段树优化,设该区间内$1$ ...

  4. Some JPR highlights (JPR 2019 March)

    Journal Name:Journal of Proteome Research Issue:2019 March Shared by: Weining Zhao 1.     Acetylome: ...

  5. 稳定UI运行结果-自动化测试失败重试和截图

    运行自动化测试的时候,有时会因为网络不稳定,测试环境或者第三方环境正在重启而造成用例运行结果不稳定,时而能跑过时而跑不过.这些难以重现的环境因素造成的用例失败会让测试人员很困扰,排查即耗费时间也没有太 ...

  6. bzoj2502清理雪道

    传送门 好题啊,由于每个点既可以进,也可以出,就可以新建一个源点和汇点,对于每个点都连边,然后就是最小流板子了. 代码: #include<cstdio> #include<iost ...

  7. github 收藏项目的方法

    1,Watching 需要收藏的项目 2,查看收藏的项目

  8. Sql 2000系统表 语句查询表结构

     SQL2000系统表的应用  –1:获取当前数据库中的所有用户表 select Name from sysobjects where xtype=’u’ and status>=0 –2:获取 ...

  9. 551 Student Attendance Record I 学生出勤纪录 I

    给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符:    'A' : Absent,缺勤    'L' : Late,迟到    'P' : Present,到场如果一个学生的出勤纪 ...

  10. 持续集成~Jenkins里的NuGet和MSBuild插件

    Jenkins是一个持续集成的环境,它是java开发的,大叔认为它的工作流程是 从源代码拉一个项目下来到它本地(可以配置定时机制) 恢复相关程序包nuget 编译程序 发布程序 现在说一下在配置jen ...