POJ【2001】Shortest Prefixes----字典树

来源:互联网 发布:域名在哪注册 编辑:程序博客网 时间:2024/05/01 21:00

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
题大意:找每个字符串唯一,且最小的前缀
code:
#include<stdio.h>#include<string.h>#include<stdlib.h>struct node{    int cs;    int jd;    struct node*next[26];};struct node*shenqing(){    struct node*p;    p=(struct node*)malloc(sizeof(struct node));    p->cs=0;    p->jd=0;    for(int i=0;i<26;i++)    {        p->next[i]=NULL;    }    return p;}void jianshu(struct node*p,char s[]){    int len=strlen(s);    struct node*tree=p;    for(int i=0;i<len;i++)    {        int ans=s[i]-'a';        if(tree->next[ans]==NULL)            tree->next[ans]=shenqing();        tree=tree->next[ans];        tree->cs++;        if(i==len-1)        tree->jd++;    }}void sc(struct node*p,char s[]){    int len=strlen(s);    struct node*tree=p;    if(tree!=NULL)    {        for(int i=0;i<len;i++)        {            int ans=s[i]-97;            if(tree->next[ans]!=NULL)            {                printf("%c",ans+'a');                tree=tree->next[ans];                if(i==len-1)                {                    tree->jd--;                    return;                }                if(tree->cs<=1)                return;            }        }    }}int main(){    struct node *tree;    tree=shenqing();    char s[1009][25];    int t=0,i,j,k;    while(scanf("%s",s[t])!=EOF)    {        jianshu(tree,s[t]);        t++;    }    for(i=0;i<t;i++)    {        printf("%s ",s[i]);        sc(tree,s[i]);        printf("\n");    }    return 0;}

0 0
原创粉丝点击