• poj 2001 Shortest Prefixes


    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

    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
    

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cstdlib>
     4 typedef struct Trie
     5 {
     6     int num;
     7     struct Trie *next[26];
     8 }Node,*trie;
     9 Node *create()
    10 {
    11     Node *node=(Node *)malloc(sizeof(Node));
    12     node->num=0;
    13     memset(node->next,0,sizeof(node->next));
    14     return node;
    15 }
    16 void insert_(trie root,char *str)
    17 {
    18     trie node=root;
    19     char *p=str;
    20     int id;
    21     while(*p)
    22     {
    23         id=*p-'a';
    24         if(node->next[id]==NULL)
    25             node->next[id]=create();
    26         node=node->next[id];
    27         ++p;
    28         node->num+=1;
    29     }
    30 }
    31 void finds(trie root,char *str)
    32 {
    33     trie node=root;
    34     char *p=str;
    35     int id;
    36     while(*p)
    37     {
    38         //printf("%c",*p);
    39         //if(node->num==1) break;
    40         //printf("%c",*p);
    41         id=*p-'a';
    42         node=node->next[id];
    43         printf("%c",*p);
    44         ++p;
    45         if(node->num==1) break;
    46     }
    47     printf("
    ");
    48 }
    49 int deal(trie T)
    50 {
    51     int i;
    52     if(T==NULL)
    53         return 0;
    54     for(i=0;i<26;i++)
    55     {
    56         if(T->next[i]!=NULL)
    57             deal(T->next[i]);
    58     }
    59     free(T);
    60     return 0;
    61 }
    62 int main()
    63 {
    64     char str[1020][30],ch;
    65     trie root=create();
    66     int k=0;
    67     while(scanf("%s",str[k])!=EOF)
    68     {
    69         insert_(root,str[k]);
    70         k++;
    71         getchar();
    72         ch=getchar();
    73         if(ch!='
    '){
    74             ungetc(ch,stdin);
    75             continue;
    76         }
    77         break;
    78     }
    79     for(int i=0;i<k;i++)
    80     {
    81         printf("%s ",str[i]);
    82         finds(root,str[i]);
    83     }
    84     deal(root);
    85     return 0;
    86 }
  • 相关阅读:
    【转载】25岁毕业,拿一万块月薪
    博客界面终于变成了自己比较满意的感觉
    梯度下降法
    WPF小试牛刀
    关于BOF改进方法的一些introduction
    POJ——1012
    这是个伟大的暑假
    上午的四个coding问题
    FindFirst,FindNext,FindClose学习
    TThread类初探
  • 原文地址:https://www.cnblogs.com/PrayG/p/5899670.html
Copyright © 2020-2023  润新知