PAT-1097 Deduplication on a Linked List

本文介绍了一个算法问题:如何从单链表中去除绝对值重复的节点,并将这些节点分离成一个新的链表。通过使用哈希表来跟踪已出现的值,并借助向量来组织输出,实现了原链表和重复元素链表的有效构建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1097 Deduplication on a Linked List (25)(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.

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

可以钻空子的题,将链表中绝对值重复的项拎出来(保留第一个 )连成新的链表,输出原链表和重复元素链表。为了方tou便lan,用hashmap模拟内存寻址,pair<>存储数据,set判断是否重复,vector顺序装入要输出的元素,分别输出就行了。

#include<stdio.h>
#include<unordered_map>
#include<utility>
#include<vector>
#include<set>
using namespace std;

unordered_map<int,pair<int,int> > mem;
int main(){
	int head,n;
	scanf("%d %d",&head,&n);
	int addr;//,v,next;
	for(int i=0;i<n;i++){
		pair<int,int> p;
		scanf("%d %d %d",&addr,&p.first,&p.second);
		mem[addr]=p;
	}
	set<int> s;vector<pair<int,pair<int,int> > > v1,v2;
	for(addr=head;addr!=-1;addr=mem[addr].second){
		if(s.find(mem[addr].first)==s.end()){//show in first time
			s.insert(mem[addr].first);
			s.insert(-mem[addr].first);
			v1.push_back(pair<int,pair<int,int> >(addr,mem[addr]));
		}else{
			v2.push_back(pair<int,pair<int,int> >(addr,mem[addr]));
		}
	}
	auto it=v1.begin();
	if(v1.size()>0){
		for(;it!=v1.end()-1;it++){
			printf("%05d %d %05d\n",it->first,it->second.first,(it+1)->first);
		}
		printf("%05d %d %d\n",it->first,it->second.first,-1);
	}
	it=v2.begin();
	if(v2.size()>0){
		for(;it!=v2.end()-1;it++){
			printf("%05d %d %05d\n",it->first,it->second.first,(it+1)->first);
		}
		printf("%05d %d %d\n",it->first,it->second.first,-1);
	}
	return 0;
}

 

### 关于Temu标签合成的功能与方法 在讨论 Temu标签合成功能之前,需明确其背景和目标。Temu 是一款跨境电商平台,专注于通过社交媒体推广商品并吸引流量。因此,在自动化发布流程中,生成适合不同社交平台的标签(如 TikTok 和 Instagram),对于提升内容的相关性和吸引力至关重要。 #### 社交媒体标签自动生成的核心逻辑 为了实现高效的标签合成功能,通常会采用自然语言处理 (NLP) 技术来分析商品描述、用户评论以及市场趋势数据。以下是可能的技术路径: 1. **关键词提取** 使用 NLP 工具(如 spaCy 或 NLTK)从商品标题、描述和其他元数据中提取核心词汇[^3]。这些工具能够识别名词短语、形容词以及其他具有代表性的词语。 2. **主题建模** 应用 LDA(Latent Dirichlet Allocation)或其他主题模型算法,将商品分类至特定的主题类别下。例如,“运动鞋”可能会被分配到“健身”或“时尚”主题中[^4]。 3. **流行标签匹配** 结合历史数据分析,筛选出当前热门且与该主题高度相关的标签集合。这一步可以通过爬取社交媒体上的实时趋势完成[^5]。 4. **个性化调整** 针对具体平台特性优化标签组合策略。比如,Instagram 更倾向于视觉导向型标签(#OOTD, #Fashionista),而 TikTok 则偏好互动性强的话题挑战类标签(#FYP, #ViralChallenge)。此过程可以参考如下伪代码片段: ```python def generate_tags(product_description, target_platform="instagram"): keywords = extract_keywords(product_description) themes = infer_themes(keywords) trending_tags = fetch_trending_hashtags(target_platform) final_tags = [] for theme in themes: matching_tag = find_best_match(theme, trending_tags) if matching_tag: final_tags.append(matching_tag) return "#".join(final_tags[:MAX_TAGS_PER_PLATFORM]) ``` #### 是否存在公开可用API? 目前并没有官方文档表明 Temu 提供专门用于标签合成的公共 API 接口。然而,开发者可以选择利用第三方服务作为替代方案,例如 Hashtagify 或 Display Purposes 等提供跨平台标签建议的服务[^6]。需要注意的是,这类外部解决方案未必完全契合内部业务需求,故定制开发可能是更优的选择。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值