Shortest Prefixes 最短前缀(c语言)

Shortest Prefixes 最短前缀(c语言),第1张

题目描述
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”.
字符串的前缀是从给定字符串的开头开始的子字符串。“碳”的前缀是:“c”,“ca”,“car”,“carb”,“carbo”和“carbon”。请注意,在此问题中,空字符串不被视为前缀,但每个非空字符串都被视为其自身的前缀。在日常语言中,我们倾向于用前缀来缩写单词。例如,“碳水化合物”通常缩写为“碳水化合物”。在此问题中,给定一组单词,您将为每个单词找到唯一标识其所表示的单词的最短前缀。

在下面的示例输入中,“碳水化合物”可以缩写为“carboh”,但不能缩写为“carbo”(或任何更短的东西),因为列表中还有其他单词以“carbo”开头。

完全匹配将覆盖前缀匹配。例如,前缀“car”与给定的单词“car”完全匹配。因此,可以毫不含糊地理解,“car”是“car”的缩写,而不是“carriage”或列表中以“car”开头的任何其他单词。
输入
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.
输入至少包含两行,但不超过 1000 行。每行包含一个由 1 到 20 个小写字母组成的单词。
输出
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.
输出包含与输入相同的行数。输出的每一行都包含输入相应行中的单词,后跟一个空格,以及唯一(无歧义)标识此单词的最短前缀。

样例输入
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
样例输出
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona

#include
#include
#include
#include
char a[1002][25];
int s=0,t=0,cb[30001][26],ca[30001];
void insert(char ch[])
{
	int w=0,l=strlen(ch+1),p=1,k;
	while(p<=l)
	   {ca[w]++;
	    k=ch[p]-'a';
	    if(!cb[w][k]) cb[w][k]=++s;
	    w=cb[w][k]; p++;
	   }
	ca[w]++;
}
void find(char ch[])
{
	int w=0,l=strlen(ch+1),p=1,k;
	while(p<=l)
	   {if(ca[w]==1&&w!=0) break;
	    k=ch[p]-'a';
	    printf("%c",ch[p]);
	    w=cb[w][k]; p++;
	   }
}
int main()
{
	while(scanf("%s",a[++t]+1)!=EOF) insert(a[t]); 
	for(int i=1;i<=t;i++)
	   {printf("%s ",a[i]+1);
	    find(a[i]);
	    printf("\n");
	   }
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/676069.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存