zoukankan      html  css  js  c++  java
  • 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<iostream>
     3 #include<cmath>
     4 #include<cstring>
     5 using namespace std;
     6 
     7 struct trie
     8 {
     9     trie* next[26];//下一个结点
    10     int num;//以当前字符串为前缀的数量
    11     trie()//构造函数
    12     {
    13         int i;
    14         for(i=0;i<26;i++)
    15             next[i]=NULL;
    16         num=0;
    17     }
    18 };
    19 
    20 trie root;
    21 
    22 void insert(char word[])
    23 {
    24     trie* r=&root;
    25     int i;
    26     for(i=0;word[i];i++)
    27     {
    28         if(r->next[word[i]-'a']==NULL)//这个字符没有
    29             r->next[word[i]-'a']= new trie;
    30         r=r->next[word[i]-'a'];
    31         r->num++;
    32     }
    33 }
    34 
    35 void find(char word[])
    36 {
    37     trie* r=&root;
    38     int i;
    39     for(i=0;word[i];i++)
    40     {
    41         printf("%c",word[i]);
    42         if(r->next[word[i]-'a']==NULL)
    43             return;
    44         r=r->next[word[i]-'a'];
    45         if(r->num==1)
    46             return;
    47     }
    48     return ;
    49 }
    50 
    51 
    52 
    53 
    54 int main()
    55 {
    56     char word[1005][25];
    57     int i=0;
    58     while(gets(word[i]))
    59     {
    60         if(word[i][0]==NULL)
    61             break;
    62         insert(word[i]);
    63         i++;
    64     }
    65     for(int ii=0;ii<i;ii++)
    66     {
    67         printf("%s ",word[ii]);
    68         find(word[ii]);
    69         printf("
    ");
    70     }
    71     return 0;
    72 }




  • 相关阅读:
    asp.net core mvc 之 DynamicApi
    打造适用于c#的feign
    asp.net App_Code文件夹相关操作
    基于Mono.Cecil的静态注入
    补充ICache
    自制简单实用IoC
    自制简单的.Net ORM框架 (一) 简介
    解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法
    微信开发之.Net
    VS2017 网站打包发布生成的文件中包含.pdb文件,解决办法
  • 原文地址:https://www.cnblogs.com/Annetree/p/7278416.html
Copyright © 2011-2022 走看看