zoukankan      html  css  js  c++  java
  • poj2001Shortest Prefixes(trie)

    Shortest Prefixes

    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 18687   Accepted: 8087

    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

    题目大意:给出一堆字符串,给每一个字符串找最小的前缀,让所有的前缀都不重复。
    字典树询问时,找到一个val为1的说明这个点只有他自己一个,所以到此结束。
    注意数组的大小
     1 #include<cstdio>
     2 #include<cstring>
     3 #define MAXN 100100
     4 
     5 char s[1010][25];
     6 
     7 struct Trie
     8 {
     9     int ch[MAXN][26];
    10     int val[MAXN];
    11     int size;
    12     Trie()
    13     {
    14         size = 1;
    15         memset(ch,0,sizeof(ch));
    16         memset(val,0,sizeof(val));
    17     }
    18     int id(char c)
    19     {
    20         return c-'a';
    21     }
    22     void Ins(char* s)
    23     {
    24         int u = 0, len = strlen(s);
    25         for (int i=0; i<len; ++i)
    26         {
    27             int c = id(s[i]);
    28             if (!ch[u][c]) ch[u][c] = size++;
    29             u = ch[u][c];
    30             val[u]++;
    31         }
    32     }
    33     void Find(char* s)
    34     {
    35         int u = 0, len = strlen(s);
    36         for (int i=0; i<len; ++i)
    37         {
    38             int c = id(s[i]);
    39             u = ch[u][c];
    40             printf("%c",s[i]);
    41             if (val[u]==1) return ;
    42         }
    43     }
    44 }t;
    45 
    46 int main()
    47 {
    48     int p = 0;
    49     while (scanf("%s",s[++p])!=EOF)
    50     {
    51         t.Ins(s[p]);
    52     }
    53     for (int i=1; i<=p; ++i)
    54     {
    55         printf("%s ",s[i]);
    56         t.Find(s[i]);
    57         printf("
    ");
    58     }
    59     return 0;
    60 }
    
    
    
     
  • 相关阅读:
    [导入]【翻译】WF从入门到精通(第十三章):打造自定义活动
    [导入]关于网页标准与JAVAScript执行的问题
    html包含html文件的方法
    [导入]C#加密方法汇总
    8、步步为营VS 2008 + .NET 3.5(8) DLINQ(LINQ to SQL)之面向对象的添加、查询、更新和删除
    [导入]【翻译】WF从入门到精通(第十五章):工作流和事务
    [导入]存储过程得到某个表的所有字段信息
    1、步步为营VS 2008 + .NET 3.5(1) VS 2008新特性之Multi Targeting(多定向)、Web Designer and CSS(集成了CSS的web设计器)和Nested Master Page(嵌套母版页)
    [导入]vbs修改注册表
    正则表达式30分钟入门教程
  • 原文地址:https://www.cnblogs.com/mjtcn/p/7125508.html
Copyright © 2011-2022 走看看