Shortest Prefixes

本文介绍了一种用于从一组单词中寻找每个单词特有的最短前缀的算法,并提供了完整的实现代码。该算法通过构建字典树来高效地解决这一问题,确保了每个单词的前缀都是唯一的。
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
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
Sample Output
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car

carbonate carbona

题目意思:输入一些单词,找出这些单词本身特有的最短前缀。

解题思路:将这些单词建树,用一个标记p->v=-1记录哪一字母是一个单词最后一个字母,然后再一次将它

们输入进去进行查找,找到一个p->=-1但之后的字母所对应的p->!=-1。于是下一个字母之前

的这些字母组成的单词就是它所特有的前缀。

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

char s[2001][21];

struct trie{
	int v;
	trie *next[26];
	trie(){
		v=0;
		for(int i=0;i<26;i++)
		{
			next[i]=NULL;
		}
	}
}; 

trie *root;

void creat(trie *root,char *str)
{
    int len=strlen(str);
	trie *p=root,*q;
	//len--;
	for(int i=0;i<len;i++)
	{
		int sign=str[i]-'a';
		if(p->next[sign]==NULL)
		{
			q=new trie();
			q->v=1;
			p->next[sign]=q;
			p=p->next[sign];
		}
		else
		{
			p->next[sign]->v++;
			p=p->next[sign];
		}
	}	
}

void find(char *str)
{
	int len=strlen(str);
	trie *p=root;
	for(int i=0;i<len;i++)
	{
		int id=str[i]-'a';
		p=p->next[id];
		if(p==NULL)
		   return;
		cout<<str[i];
		if(p->v==1)
		{
			cout<<endl;
			return;
		}
	}
	cout<<endl;
}

void del(trie *root)
{
	for(int i=0;i<26;i++)
	{
		if(root->next[i])
		{
			del(root->next[i]); 
		}
	}
	delete(root);
}

int main()
{
	int size=0;
	root=new trie();
	while(~scanf("%s",s[size]))
	{
		creat(root,s[size]);
		size++;
	}
	for(int i=0;i<size;i++)
	{
		cout<<s[i]<<" ";
		find(s[i]);
	}
	return 0;
}



### 解题思路 可以使用字典树(Trie树)来解决该问题。Trie树的本质是将单词之间的公共前缀合并起来,在单词的结尾处设置一个标识符,表示该字符为一个单词的结束。通过构建Trie树,可以统计每个前缀的出现次数,对于每个单词,从其第一个字符开始逐步增加前缀长度,直到找到一个仅在该单词中出现的前缀,这个前缀即为该单词的唯一最短前缀。 ### 代码实现 ```python class TrieNode: def __init__(self): # 存储子节点,键为字符,值为对应的TrieNode self.children = {} # 记录该节点被经过的次数 self.count = 0 class Trie: def __init__(self): # 初始化根节点 self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] # 每经过一个节点,该节点的计数加1 node.count += 1 def find_shortest_prefix(self, word): node = self.root prefix = "" for char in word: prefix += char node = node.children[char] # 当该前缀的计数为1时,说明该前缀是唯一的 if node.count == 1: return prefix return word def get_shortest_prefixes(words): trie = Trie() # 向Trie树中插入所有单词 for word in words: trie.insert(word) shortest_prefixes = [] # 为每个单词找到唯一最短前缀 for word in words: prefix = trie.find_shortest_prefix(word) shortest_prefixes.append((word, prefix)) return shortest_prefixes # 读取输入 words = [] while True: try: word = input() if not word: break words.append(word) except EOFError: break # 获取每个单词的唯一最短前缀 result = get_shortest_prefixes(words) # 输出结果 for word, prefix in result: print(f"{word} {prefix}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值