1097. Deduplication on a Linked List (25)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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
1 #include<stdio.h> 2 #include<math.h> 3 #include<set> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 struct node 8 { 9 int val; 10 int next,id; 11 }; 12 node Link[100100]; 13 int main() 14 { 15 int head,n,id,val,next; 16 scanf("%d%d",&head,&n); 17 for(int i = 0 ;i < n ;++i) 18 { 19 scanf("%d%d%d",&id,&val,&next); 20 Link[id].val = val; 21 Link[id].next = next; 22 Link[id].id = id; 23 } 24 int p = head; 25 set<int> ss; 26 vector<node> vv,vv2; 27 while(p != -1) 28 { 29 int tem = abs(Link[p].val); 30 if(ss.count(tem) == 0) 31 { 32 ss.insert(tem); 33 vv.push_back(Link[p]); 34 } 35 else vv2.push_back(Link[p]); 36 p = Link[p].next; 37 } 38 p = head; 39 for(int i = 0 ; i < (int)vv.size() - 1;++i) 40 { 41 printf("%05d %d %05d\n",vv[i].id,vv[i].val,vv[i+1].id); 42 } 43 if(vv.size() > 0) 44 printf("%05d %d -1\n",vv[vv.size()-1].id,vv[vv.size()-1].val); 45 46 for(int i = 0 ;i < (int)vv2.size() -1;++i) 47 { 48 printf("%05d %d %05d\n",vv2[i].id,vv2[i].val,vv2[i+1].id); 49 } 50 if(vv2.size() > 0) 51 printf("%05d %d -1\n",vv2[vv2.size()-1].id,vv2[vv2.size()-1].val); 52 return 0; 53 }
以上是关于1097. Deduplication on a Linked List (25)的主要内容,如果未能解决你的问题,请参考以下文章
1097 Deduplication on a Linked List
1097. Deduplication on a Linked List (25)
1097. Deduplication on a Linked List (25)
PAT1097:Deduplication on a Linked List
PAT 1097. Deduplication on a Linked List (链表)
1097. Deduplication on a Linked List (25)链表——PAT (Advanced Level) Practise