首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【POJ】2001 - Shortest Prefixes(字典树)

【POJ】2001 - Shortest Prefixes(字典树)

作者头像
FishWang
发布2025-08-26 15:04:34
发布2025-08-26 15:04:34
9200
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

Shortest Prefixes

Time Limit: 1000MS

Memory Limit: 30000K

Total Submissions: 17357

Accepted: 7551

Description

A prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c", "ca", "car", "carb", "carbo", and "carbon". Note that the empty string is not considered a prefix in this problem, but every non-empty string is considered to be a prefix of itself. In everyday language, we tend to abbreviate words by prefixes. For example, "carbohydrate" is commonly abbreviated by "carb". In this problem, given a set of words, you will find for each word the shortest prefix that uniquely identifies the word it represents. In the sample input below, "carbohydrate" can be abbreviated to "carboh", but it cannot be abbreviated to "carbo" (or anything shorter) because there are other words in the list that begin with "carbo". An exact match will override a prefix match. For example, the prefix "car" matches the given word "car" exactly. Therefore, it is understood without ambiguity that "car" is an abbreviation for "car" , not for "carriage" or any of the other words in the list that begins with "car".

Input

The input contains at least two, but no more than 1000 lines. Each line contains one word consisting of 1 to 20 lower case letters.

Output

The output contains the same number of lines as the input. Each line of the output contains the word from the corresponding line of the input, followed by one blank space, and the shortest prefix that uniquely (without ambiguity) identifies this word.

Sample Input

代码语言:javascript
代码运行次数:0
运行
复制
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona

Source

Rocky Mountain 2004

估计再写两道题我的字典树风格就确定了,个人感觉字典树还是要用灵活,不是每个变量都有固定的含义,要随机应变。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
struct Trie
{
	Trie *next[26];
	int v;
	void init()
	{
		v = 0;
		for (int i = 0 ; i < 26 ; i++)
			next[i] = NULL;
	}
};
Trie *root;
int idx(char c)
{
	return c - 'a';
}
void insert(char s[][22],int x)
{
	int l = strlen(s[x]);
	Trie *p = root , *q;
	for (int i = 0 ; i < l ; i++)
	{
		int id = idx(s[x][i]);
		if (p->next[id] == NULL)
		{
			q = (Trie *)malloc(sizeof(Trie));		//开新指针 
			q->init();
			p->next[id] = q;
		}
		p = p->next[id];
		p->v++;
	}
}
void min_permix(char s[][22],int x)		//查询最小前缀 
{
	printf ("%s ",s[x]);
	int l = strlen(s[x]);
	Trie *p = root;
	for (int i = 0 ; i < l ; i++)
	{
		printf ("%c",s[x][i]);
		int id = idx(s[x][i]);
		p = p->next[id];
		if (p->v == 1)		//如果字母单独出现说明已经出现最小前缀了,结束
			break;
	}
	printf ("\n");
}
int main()
{
	char s[1011][22];
	int ant = 0;
	root = (Trie *)malloc(sizeof(Trie));
	root->init();
	while (~scanf ("%s",s[ant++]))
		insert(s,ant-1);
	for (int i = 0 ; i < ant ; i++)
		min_permix(s , i);
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档