PAT甲级1097 Deduplication on a Linked List (25 分)

本文提供了一道PAT在线评测系统中关于链表数据结构的题解,详细介绍了如何去除链表中绝对值重复的节点,并附上了完整的C++代码实现。通过使用静态数组和布尔数组来标记节点值的首次出现,实现了链表的去重并分别输出了去重后的链表和被移除的节点链表。

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

 

      题目大意:

         给出一个起始地址以及n结点,随后n行即是结点地址、值以及下个结点的地址。要求将绝对值相同的结点剔除出去,只保留第一个结点。要求输出新链表以及删除的链表。

 

 

思路:

         静态数组问题:用数组下标作为结点地址,存储值与下个结点的地址。

         其次遍历数组,用一个数组作为该结点值是否初次出现, 存入输出链表ans。否则存入删除链表dl。

       有个特别重要的,而且容易的忽略的知识点,关于PAT段错误的深层原因:https://blog.youkuaiyun.com/qq_42306885/article/details/99606600

 

参考代码:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
	int v, ad;
}temp[1000000];
vector<node>ans, dl;
bool ft[100000];
int main(){
	int st, n;
	scanf("%d%d", &st, &n);
	for(int i = 0; i < n; ++i){
		int ad;
		scanf("%d", &ad);
		scanf("%d%d", &temp[ad].v, &temp[ad].ad);
	}
	while(st != -1){
		if(!ft[abs(temp[st].v)]){
			ft[abs(temp[st].v)] = true;
			ans.push_back({temp[st].v, st});
		}else	dl.push_back({temp[st].v, st});
		st = temp[st].ad;
	}
	for(int i = 0; i + 1< ans.size(); ++i)	printf("%05d %d %05d\n", ans[i].ad, ans[i].v, ans[i + 1].ad);
	if(ans.size() != 0)		printf("%05d %d -1\n", ans[ans.size() - 1].ad, ans[ans.size() - 1].v);
	for(int i = 0; i + 1< dl.size(); ++i)	printf("%05d %d %05d\n", dl[i].ad, dl[i].v, dl[i + 1].ad);
	if(dl.size() != 0)		printf("%05d %d -1", dl[dl.size() - 1].ad, dl[dl.size() - 1].v);
	return 0;
}

 

 

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端离系统,将AOA应用于图像割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像割模块,引入更复杂的预处理或后处理技术以提升割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值