PAT(甲级)1097
来源:互联网 发布:vscode代码格式化 编辑:程序博客网 时间:2024/06/06 05:45
1097. Deduplication on a Linked List (25)
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 <iostream>#include <cstring>#define SIZE 100005#define LIMIT 10004using namespace std;////////////////////////////////////////////////we can also print the new linklist in the same time to deduplication////////////////////////////////////////////////////////////struct Node{int value;int address;};Node records[SIZE];Node removed[SIZE];bool flag[LIMIT];int main(){memset(flag,false,sizeof(flag));int start,N;//freopen("test.txt","r",stdin);scanf("%d%d",&start,&N);int index;for(int i = 0;i<N;i++){scanf("%d",&index);scanf("%d%d",&records[index].value,&records[index].address);}int k=0;int iter=start;int previous,count=0;int index1;while(iter !=-1){index1= index = records[iter].value;if(index <0) index = -index;if(!flag[index]){flag[index] = true;previous = iter;count++;}else{removed[k].address = iter;removed[k].value = index1;k++;records[previous].address = records[iter].address; //modify next address}iter = records[iter].address;}iter = start;while(count-- > 1){int Next_addr= records[iter].address;printf("%05d %d %05d\n",iter,records[iter].value,Next_addr);iter = Next_addr;}printf("%05d %d %d\n",iter,records[iter].value,-1);if(k){ //no duplicationsiter= 0;while(k-- >1){int Next_addr = removed[iter+1].address;printf("%05d %d %05d\n",removed[iter].address,removed[iter].value,Next_addr);iter++;}printf("%05d %d %d\n",removed[iter].address,removed[iter].value,-1); }//fclose(stdin);return 0;}
- PAT(甲级)1097
- 浙大PAT甲级 1097
- PAT甲级1097
- PAT 甲级
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- PAT(甲级)1013
- PAT(甲级)1014
- C#中关于@的用法
- Jackson 高性能的JSON处理 ObjectMapper
- 图形验证码one
- JavaScript:instanceof
- LeetCode题解——Add Digits
- PAT(甲级)1097
- JSP解析JSONObject类型对象出现null object异常解决方案
- Mybatis系列(二)配置
- spring使用手动装载依赖的bean
- PAT(甲级)1098
- JS Map 和 List 的简单实现代码
- JavaScript:继承
- hdu1199 初学离散化的一点心得
- Activity的四种加载模式