PAT (Advanced Level) Practice 1097 Deduplication on a Linked List (25分) (静态链表+测试实例)

1.题目

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 (≤10​5​​) 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 10​4​​, 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

2.题目分析 

1.使用数组标记,第一次出现的放在ori中,重复的放在dedup中,并同时更新ori数组中最后一个元素的next

注意:5位address以及next;全重复、无重复

3.代码

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
struct node
{
	int address;
	int data;
	int next;
};
int mark[100010];
int main()
{
	int headadd, n,a;
	scanf("%d %d", &headadd, &n);
	vector<node>list(100010);
	vector<node>ori;
	vector<node>dedup;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a);
		scanf("%d %d", &list[a].data, &list[a].next);
		list[a].address = a;
	}
	int temp = headadd;
	while (temp != -1)
	{
		if (mark[abs(list[temp].data)] == 0) { ori.push_back(list[temp]); mark[abs(list[temp].data)] = 1; }
		else
		{
			ori.back().next = list[temp].next;
			dedup.push_back(list[temp]);
		}
		temp = list[temp].next;
	}
	if (ori.size() > 1)
	{
		printf("%05d %d %05d\n", ori[0].address, ori[0].data, ori[0].next);
		int i;
		for ( i = 1; i < ori.size()-1; i++)
		{
			printf("%05d %d %05d\n", ori[i].address, ori[i].data, ori[i].next);
		}
		printf("%05d %d -1\n", ori[i].address, ori[i].data);
	}
	else
		printf("%05d %d -1\n", ori[0].address, ori[0].data);
	if (dedup.size() > 1)
	  {
		int i;
		printf("%05d %d %05d\n", dedup[0].address, dedup[0].data, dedup[1].address);
		for ( i = 1; i < dedup.size()-1; i++)
		{
			printf("%05d %d %05d\n", dedup[i].address, dedup[i].data, dedup[i+1].address);
		}
		printf("%05d %d -1\n", dedup[i].address, dedup[i].data);
	}
	else if(dedup.size()==1)
		printf("%05d %d -1", dedup[0].address, dedup[0].data);

}

// 00100 4
// 99999 -7 -1
// 23854 -15 00000
// 00000 -15 99999
// 00100 21 23854

// 00100 5
// 00100 15 23854
// 23854 -15 00000
// 00000 -15 99999
// 99999 -15 87654
// 87654 15 -1


// 00100 5
// 99999 -7 87654
// 23854 -16 00000
// 87654 15 -1
// 00000 -17 99999
// 00100 21 23854
发布了203 篇原创文章 · 获赞 4 · 访问量 5689

猜你喜欢

转载自blog.csdn.net/qq_42325947/article/details/105441502