PAT 甲级 A1097

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.

输入格式

Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (≤ 1 0 5 10^{5} 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 1 0 4 10^{4} 104​​, and Next is the position of the next node.

输出格式

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. 题意大概是让我们把链表中重复的结点删除,只保留第一个出现的。所以可以用一个map或数组来标记结点是否出现。
  2. 我用了两种方式解这题。解法一新开了一个数组removeL保存需要移除的结点,遍历原来的链表时,利用一个指针pre指向非重复链表的前一个结点,p指向当前结点,结点需要移除时,修改这两个指针,并把移除的结点移动到removeL数组中。
  3. 解法二更好理解,写一个三级排序的cmp+sort实现。先将有效结点移动到最左边,然后再把不需要删除的结点移动到左边,最后按结点递增顺序排序。在结点中设置多几个标记位,其中flag表示结点是否为链表中的结点,isRemove表示改结点是否是需要移除的结点,order是该结点在链表中的序号,直接调用sort函数排序就行了~

AC代码

解法一

#include <iostream>
#include<math.h>
#include<unordered_map>
using namespace std;
struct node {
	int addr, data, next;
}linkList[100005];
node removeL[100005];
int main(){
	int s, n, addr, vailedN = 0, key, len2 = 0;
	unordered_map<int, bool> isDupicated;
	scanf("%d %d", &s, &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &addr);
		linkList[addr].addr = addr;
		scanf("%d %d", &linkList[addr].data, &linkList[addr].next);
	}
	for (int pre = -1, p = s; p != -1;) {  //pre为前一个不重复的结点
		key = abs(linkList[p].data);
		if (isDupicated.find(key) == isDupicated.end()) {
			isDupicated[key] = true;
			pre = p;
			p = linkList[p].next;
		}
		else {
			while (1) { //直到下个存在不重复的key跳出
				removeL[len2++] = linkList[p];
				p = linkList[p].next;
				key = abs(linkList[p].data);
				if (isDupicated.find(key) == isDupicated.end()||p==-1) break;
			}
			linkList[pre].next = p;  //修改链表
		}
	}
	for (int p = s; p != -1; p = linkList[p].next) {//打印修改后的链表
		if (linkList[p].next != -1) {
			printf("%05d %d %05d\n", p, linkList[p].data, linkList[p].next);
		}
		else printf("%05d %d -1\n", p, linkList[p].data);
	}
	if (len2 > 0) { //打印移除的结点
		for (int i = 0; i < len2 - 1; i++) {
			printf("%05d %d %05d\n", removeL[i].addr, removeL[i].data, removeL[i + 1].addr);
		}
		printf("%05d %d -1\n", removeL[len2 - 1].addr, removeL[len2 - 1].data);
	}
	return 0;
}

解法二

#include <iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct node {
	int order, addr, data, next;
	bool flag, isRemove;
}linkList[100005];
bool map[10005] = { 0 };
bool cmp(node a, node b) {
	if (a.flag != b.flag) return a.flag > b.flag;
	else if (a.isRemove != b.isRemove) {
		return a.isRemove < b.isRemove;
	}
	return a.order < b.order;
}
void showResult(node*L, int i, int j) {
	if (j - i > 0) {
		for (int k = i; i < j - 1; i++) {
			printf("%05d %d %05d\n", L[i].addr, L[i].data, L[i + 1].addr);
		}
		printf("%05d %d -1\n", L[j - 1].addr, L[j - 1].data);
	}
}
int main() {
	int s, n, addr, vailedN = 0, key, len1 = 0, len2 = 0;
	scanf("%d %d", &s, &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &addr);
		linkList[addr].addr = addr;
		scanf("%d %d", &linkList[addr].data, &linkList[addr].next);
	}
	for (int p = s; p != -1; p = linkList[p].next) {
		key = abs(linkList[p].data);
		linkList[p].flag = true;
		if (map[key] == false) {
			linkList[p].order = len1++;
			map[key] = true;
		}
		else {
			linkList[p].order = len2++;
			linkList[p].isRemove = true;
		}
	}
	sort(linkList, linkList + 100005, cmp);
	showResult(linkList, 0, len1);
	showResult(linkList, len1, len1 + len2);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值