pat1097. Deduplication on a Linked List (25)

來源:互聯網
上載者:User

標籤:

1097. Deduplication on a Linked List (25)時間限制300 ms記憶體限制65536 kB代碼長度限制16000 B判題程式Standard作者CHEN, Yue

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 23854
Sample Output:
00100 21 2385423854 -15 9999999999 -7 -100000 -15 8765487654 15 -1

提交代碼

 

為了方便,要加頭指標。

 1 #include<cstdio> 2 #include<stack> 3 #include<algorithm> 4 #include<iostream> 5 #include<stack> 6 #include<set> 7 #include<map> 8 using namespace std; 9 struct ndoe{10     int k,next;11 };12 ndoe mem[100005];13 bool ha[10005];14 int abs(int a){15     if(a<0){16         a=-a;17     }18     return a;19 }20 #define refirst 10000121 #define nrfirst 10000222 int main(){23     //freopen("D:\\INPUT.txt","r",stdin);24     int n,first;25     scanf("%d %d",&first,&n);26     int i,cur;27     for(i=0;i<n;i++){28         scanf("%d",&cur);29         scanf("%d %d",&mem[cur].k,&mem[cur].next);30     }31     cur=nrfirst;32     int relist=refirst,p=first;33     while(p!=-1){34         while(p!=-1&&ha[abs(mem[p].k)]){35             mem[relist].next=p;36             relist=p;37             p=mem[p].next;38         }39         if(p!=-1){40             ha[abs(mem[p].k)]=true;41         }42         mem[cur].next=p;43         if(p!=-1){44             cur=p;45             p=mem[cur].next;46         }47     }48     mem[relist].next=-1;49     p=mem[nrfirst].next;50     while(p!=-1){51         if(mem[p].next!=-1){52             printf("%05d %d %05d",p,mem[p].k,mem[p].next);53         }54         else{55             printf("%05d %d %d",p,mem[p].k,mem[p].next);56         }57         printf("\n");58         p=mem[p].next;59     }60     p=mem[refirst].next;61     while(p!=-1){62         if(mem[p].next!=-1){63             printf("%05d %d %05d",p,mem[p].k,mem[p].next);64         }65         else{66             printf("%05d %d %d",p,mem[p].k,mem[p].next);67         }68         printf("\n");69         p=mem[p].next;70     }71     return 0;72 }

 

pat1097. Deduplication on a Linked List (25)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.