poj 2001 Shortest Prefixes 字典树找前缀

来源:互联网 发布:js symbol 编辑:程序博客网 时间:2024/05/17 23:10

首次认识到Trie树的强大之处!简单易懂,只要对建立一般的树的方法有所了解就OK了。Trie树,又称单词查找树键树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。

它有3个基本特性:

  1)根节点不包含字符,除根节点外每一个节点都只包含一个字符。

  2)从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。

  3)每个节点的所有子节点包含的字符都不相同。


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

carbohydratecartcarburetorcaramelcariboucarboniccartilagecarboncarriagecartoncarcarbonate

Sample Output

carbohydrate carbohcart cartcarburetor carbucaramel caracaribou caricarbonic carbonicartilage carticarbon carboncarriage carrcarton cartocar carcarbonate carbona
#include<stdio.h>#include<string.h>#include<stdlib.h>char s[1010][22];struct node{    int a;    struct node *next[26];}*head,*p,*q;void creat(int i){    int t,j,l,k;    l=strlen(s[i]);    p=head;    for(t=0; t<l; t++)    {        k=s[i][t]-'a';        if(p->next[k]==NULL)        {            q=(struct node*)malloc(sizeof(struct node));            for(j=0; j<26; j++)                q->next[j]=NULL;            q->a=1;            p->next[k]=q;            p=q;        }        else        {            p=p->next[k];            p->a++;        }    }}void charzhao(int t){    int i,j,l,k;    p=head;    l=strlen(s[t]);    for(i=0; i<l; i++)    {        k=s[t][i]-'a';        p=p->next[k];        printf("%c",s[t][i]);        if(p->a==1)        {            break;        }    }    printf("\n");}int main(){    int i,j;    head=(struct node*)malloc(sizeof(struct node));    for(i=0; i<26; i++)        head->next[i]=NULL;    i=0;    while(~scanf("%s",s[i]))    {        creat(i);        i++;    }    for(j=0; j<i; j++)    {      printf("%s ",s[j]);        charzhao(j);    }}
好长时间没有用字典树了,竟然手生了,
0 0
原创粉丝点击