poj2001Shortest Prefixes(字典树)
来源:互联网 发布:java 取小数点后两位 编辑:程序博客网 时间:2024/04/30 05:56
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".
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
carbohydratecartcarburetorcaramelcariboucarboniccartilagecarboncarriagecartoncarcarbonate
Sample Output
carbohydrate carbohcart cartcarburetor carbucaramel caracaribou caricarbonic carbonicartilage carticarbon carboncarriage carrcarton cartocar carcarbonate carbona
#include<stdio.h>#include<string.h>typedef struct nn{ int flag; struct nn *next[26];}node;node *builde(){ node *p=new node; p->flag=0; for(int i=0;i<26;i++) p->next[i]=NULL; return p;}node *root;void insert(char str[]){ node *p=root; for(int i=0;str[i]!='\0';i++) { if(p->next[str[i]-'a']==NULL) p->next[str[i]-'a']=builde(); p=p->next[str[i]-'a']; p->flag++; }}void find(char str[]){ node *p=root; printf("%s ",str); for(int i=0;str[i]!='\0';i++) { if(p->next[str[i]-'a']==NULL) break ; printf("%c",str[i]); p=p->next[str[i]-'a']; if(p->flag==1) break ; } printf("\n");}int main(){ char str[2005][25]; int n=0; root=builde(); while(scanf("%s",str[n])>0) { insert(str[n]); n++; } for(int i=0;i<n;i++) find(str[i]);}
0 0
- poj2001Shortest Prefixes(字典树)
- poj2001Shortest Prefixes【字典树找最短唯一前缀】
- POJ2001Shortest Prefixes
- poj2001Shortest Prefixes
- Shortest Prefixes(字典树)
- POJ2001_Shortest Prefixes(字典树)
- POJ2001 - Shortest Prefixes - 字典树
- Shortest Prefixes(字典树)
- Shortest Prefixes(字典树)
- POJ_2001_Shortest Prefixes(字典树)
- POJ2001 Shortest Prefixes【字典树】
- Shortest Prefixes(字典树)
- 【poj2001】Shortest Prefixes 字典树
- POJ2001 Shortest Prefixes(字典树)
- Shortest Prefixes (POJ_2001) 字典树
- Shortest Prefixes 字典树模板
- zju2346 Shortest Prefixes(字典树 trie)
- POJ 2001:Shortest Prefixes(字典树)
- 我的恋人
- 你静静地居住在我的心里
- 祈祷
- 心中的枫叶
- 人生-如花,如水,如画,如歌
- poj2001Shortest Prefixes(字典树)
- 无奈的心
- 心痛
- 淡淡的
- 感悟生命
- 无奈的思绪
- 曾经
- 春风的心愿
- I LOVU U !I MISS U !