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. win7 mongod不是内部命令

    1.下载MongoDB 1.1 MongoDB下载 1.2 选择Server下面的 Community 2.安装MongoDB 2.1 注意事项:一直下一步就行了,但是遇到下面这个界面,注意一定要去掉 ...

  2. 适合新手看的超详细CentOS Linux 7 安装Tomcat8过程

    非常详细的安装Tomcat8的步骤,适合新手学习.废话不多说,直接干! 前提条件 1. 已有可直接连接的CentOS7系统 2. CentOS7系统已安装Java JDK 8 下载Tomcat8 下载 ...

  3. C/C++函数调用过程分析

    http://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601204.html 这里以一个简单的C语言代码为例,来分析函数调用过程 代码: #in ...

  4. bzoj 2406: 矩阵【二分+有源汇上下界可行流】

    最大值最小,所以考虑二分 |Σaij-Σbij|<=mid,所以Σbij的上下界就是(Σaij-mid,Σaij+mid) 考虑建有上下界网络,连接(s,i,Σaik-mid,Σaik+mid) ...

  5. Mol Cell Proteomics. |陈洁| 整合鸟枪法蛋白质组学中鉴定和定量的错误率

    大家好,本周分享的是发表在MCP上的一篇关于鸟枪蛋白质组学中的错误率的文章,题目是Integrated identification and quantification error probabil ...

  6. 了解cookie

    1.cookie数据会自动在Web浏览器和Web服务器之间传输的,因此服务端脚本就可以读,写存储在客户端的cookie值. 2.在javascript中使用cookie不会采用任何加密机制,因此是不安 ...

  7. sessionStorage 、localStorage

    localStorage和sessionStorage使用时使用相同的API: localStorage.setItem("key","value");//以“ ...

  8. STP-1-802.1D生成树协议及改进

    第一个 IEEE 标准化的STP也常称为“ 传统”STP,最初在802. 1D中进行了描述. 之后得到了改进,发布在所谓的修正案中:快速 STP( RSTP),在修正案802. 1w 中描述了它的标准 ...

  9. Codeforces Round #532(Div. 2) B.Build a Contest

    链接:https://codeforces.com/contest/1100/problem/B 题意: 给n,m. 给m个数,每个数代表一个等级,没给一个将其添加到题目池中,当题目池中有难度(1-n ...

  10. Codeforces Round #541 (Div. 2) C.Birthday

    链接:https://codeforces.com/contest/1131/problem/C 题意: 求给的n个数,相邻差值最小的排列方式.1-n相邻. 思路: sort后隔一个取一个,取到底后再 ...