Shortest Prefixes 字典树 (前缀树) 入门题 题解

Shortest Prefixes 题解

  • (1)题目
  • (2)题解

(1)题目

                           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

(2)题解

这道题的题意是 输出最短的前缀唯一地(没有歧义)标识这个单词。字典树的入门题,只需要在原来的模板上改动一些即可。解题思路是 输出在字典中有相同的前缀+1个字符来唯一识别这个单词。
字典树(前缀树)的模板:
https://blog.csdn.net/u011787119/article/details/46991691
代码如下:

#include
#include
#include
using namespace std;
const int maxn = 26;
char s[1010][30];
struct Trie
{
     
	Trie *Next[maxn];
	int cnt;
	Trie()
	{
     
		cnt = 1;
		memset(Next, NULL, sizeof(Next));
	}
}*root;
void insert(char *str)
{
     
	int len = strlen(str);
	Trie *p = root, *q;
	for (int i = 0; i < len; i++)
	{
     
		int id = str[i] - 'a';
		if (p->Next[id] == NULL)
		{
     
			q = new Trie();
			p->Next[id] = q;
			p = p->Next[id];
		}
		else
		{
     
			p=p->Next[id];
			(p->cnt)++;
		}
	}
}
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->cnt > 1)  ///输出这些字符串相同的前缀
		{
     
			printf("%c", str[i]);
		}
		else   ///当p->cnt==1时,多输出一个字符来唯一的标识这个字符串(本题的关键)      
		{
     
			printf("%c", str[i]);
			return;
		}
	}
}
int main()
{
     
	root = new Trie();
	int n = 0;  //模仿别人的博客来进行输入
	while (scanf("%s", s[n])!=EOF)
	{
     
		insert(s[n]);
		n++;
	}
	for (int i = 0; i < n; i++)
	{
     
		printf("%s ", s[i]);
		find(s[i]);
		printf("\n");
	}
	return 0;
}

这道题参考的这篇博客
链接: https://blog.csdn.net/caduca/article/details/43531875.

你可能感兴趣的:(字符串)