PAT(甲级)1097

来源:互联网 发布:vscode代码格式化 编辑:程序博客网 时间:2024/06/06 05:45

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

#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;}

0 0
原创粉丝点击