Pat 甲级 训练真题集 1097!

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 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

题意为分离出有重复值的链表,使其为无重复值得链表

解题思路,利用struct 装节点信息,利用set排除重复值

分别用list与v来保存剩下的与移除的节点地址

对list与v更新next

最后将list与v分别打印出来

代码如下:

#include<iostream>
#include<set>
#include<vector>
using namespace std;

struct Node{ 
	int key;
	int next;
}node[100000];
int main(){
	int first,n,current,key,next;
	scanf("%d%d",&first,&n);
	for(int i=0;i<n;++i){
		scanf("%d%d%d",&current,&key,&next);
		node[current].key=key;
		node[current].next=next;
	}
	if(first==-1){
		return 0;
	}
	set<int> already;
	int index=first;
	if(node[index].next==-1){
		printf("%05d %d -1",index,node[index].key);
		return 0;
	}
	vector<int> v;
	vector<int> list;
	while(index!=-1){
		int absolute=node[index].key>0?node[index].key:node[index].key-2*node[index].key;
		if(already.find(absolute)==already.end()){
			already.insert(absolute);
			list.push_back(index);
		}else{
			v.push_back(index);		
		}
		index=node[index].next;
	}
	for(int i=0;i<list.size()-1;++i){
		if(node[list[i]].next!=list[i+1]){
			node[list[i]].next=list[i+1];
		}
	}
	node[list[list.size()-1]].next=-1;
	for(int i=0;i<v.size()-1;++i){
		if(node[v[i]].next!=v[i+1]){
			node[v[i]].next=v[i+1];
		}
	}
	node[v[v.size()-1]].next=-1;
	for(int i=0;i<list.size();++i){
		if(i!=list.size()-1){
			printf("%05d %d %05d\n",list[i],node[list[i]].key,node[list[i]].next);
		}else{
			printf("%05d %d -1\n",list[i],node[list[i]].key);
		}
	}
	for(int i=0;i<v.size();++i){
		if(i!=v.size()-1){
			printf("%05d %d %05d\n",v[i],node[v[i]].key,node[v[i]].next);
		}else{
			printf("%05d %d -1\n",v[i],node[v[i]].key);
		}
	}
	return 0;
}

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦