1097. Deduplication on a Linked List (25)
来源:互联网 发布:威尼斯 知乎 编辑:程序博客网 时间:2024/06/11 10:50
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 599999 -7 8765423854 -15 0000087654 15 -100000 -15 9999900100 21 23854Sample Output:
00100 21 2385423854 -15 9999999999 -7 -100000 -15 8765487654 15 -1
把绝对值未出现过的地址放到一个数组,出现过的放到另一个数组,分别输出即可。
注意;
链表上只有一个结点的情况。
#include<stdio.h>#include<math.h>struct node{int x;int next;}stu[100000];int map[10010];int order1[100000];int order2[100000];int main(){int start,n,i,s,next,x;scanf("%d %d",&start,&n);for(i=0;i<n;i++){scanf("%d %d %d",&s,&x,&next);stu[s].x=x;stu[s].next=next;}int p=start;int cou1=0,cou2=0;while(p!=-1){if(map[abs(stu[p].x)]==0){map[abs(stu[p].x)]=1;order1[cou1++]=p;}else{order2[cou2++]=p;}p=stu[p].next;}if(cou1==1){//只有一个点!!!测试点2 printf("%05d %d -1\n",order1[0],stu[order1[0]].x);return 0;}for(i=0;i<cou1-1;i++){printf("%05d %d %05d\n",order1[i],stu[order1[i]].x,order1[i+1]);}printf("%05d %d -1\n",order1[i],stu[order1[i]].x);for(i=0;i<cou2-1;i++){printf("%05d %d %05d\n",order2[i],stu[order2[i]].x,order2[i+1]);}printf("%05d %d -1\n",order2[i],stu[order2[i]].x);}
- 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)
- 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)
- 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)
- 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)
- 1097. Deduplication on a Linked List (25)
- 1097. Deduplication on a Linked List (25)
- cocosstudio3.0以上版本控件高级属性之用户数据的使用
- Android studio打包签名报错:Error:(3) Error: "app_name" is not translated in "am" (Amharic);
- jmeter录制移动app脚本
- java中子类的调用
- MFC子窗口向父窗口发送消息(测试成功20110117)
- 1097. Deduplication on a Linked List (25)
- 超级课程表课表的界面的实现
- [ax]WebBrowser.ExecWB(OLECMDID cmdID, OLECMDEXECOPT cmdexecopt) 参数列表及说明
- Win7 64bit 安装tensorflow,使用集成环境Anaconda和Pycharm
- HDU-2533:Escape
- 百度编辑器出现实体字符
- 二、矩阵的掩膜操作
- 前端学习
- 将含有pandas、numpy 等c源码包的代码打包为exe时出现错误的解决方法