zoukankan      html  css  js  c++  java
  • poj 2001 Shortest Prefixes(字典树trie 动态分配内存)

    Shortest Prefixes
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 15610   Accepted: 6734

    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
    

    Source

    题意:给不超过1000个单词,输出能代表这个单词的唯一前缀,若没有則直接把整个单词输出~输出格式详见output~

    trie,基本的数据结构,个人认为就是链表,不过是树形的而已~

    ps: 被读数据坑到了~ 刚开始写的是 while(scanf("%s",bank[n++]) != -1) { inserts(bank[n-1]); } ,然后就各种WA,唉~

    还好努力找到了错误~ 改成了while(scanf("%s",bank[n]) != -1){inserts(bank[n]); n++; },就神奇的过了,可能多个回车什么的导致错误吧。

     1 #include<iostream>
     2 #include<vector>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cstdlib>
     6 #include <math.h>
     7 #include<algorithm>
     8 #define ll long long
     9 #define eps 1e-8
    10 using namespace std;
    11 
    12 struct nodes
    13 {
    14     int cnt;
    15     struct nodes *next[26];//26个后继
    16     nodes()//结构体内的初始化函数
    17     {
    18         int i;
    19         cnt = 0;
    20         for(i = 0; i < 26; i++)
    21             next[i] = NULL;
    22     }
    23 } root,*temp;
    24 
    25 void inserts(char *word)
    26 {
    27     nodes *cur = &root;
    28     while(*word )
    29     {
    30         int t = *word - 'a';
    31         if(cur->next[t] == NULL)
    32         {
    33             temp = (nodes *)malloc(sizeof(nodes));//动态申请节点
    34             temp->cnt = 0;
    35             for(int i = 0; i < 26; i++)
    36                 temp->next[i] = NULL;
    37             cur->next[t] = temp;
    38         }
    39         cur = cur->next[t];
    40         cur->cnt++;
    41         word++;
    42     }
    43 }
    44 
    45 void searchs(char *word)
    46 {
    47     nodes *cur = &root;
    48     while(*word && cur)//结束条件,缺一不可
    49     {
    50         if(cur->cnt == 1) break;
    51         printf("%c",*word);
    52         cur = cur->next[*word - 'a'];
    53         word++;
    54     }
    55     printf("
    ");
    56 }
    57 
    58 int main(void)
    59 {
    60     char bank[1005][22];
    61     int i,n = 0;
    62 
    63     while(scanf("%s",bank[n]) != -1)
    64     {
    65         inserts(bank[n]);
    66         n++;
    67     }
    68     for(i = 0; i < n; i++)
    69     {
    70         printf("%s ",bank[i]);
    71         searchs(bank[i]);
    72     }
    73     return 0;
    74 }
  • 相关阅读:
    搜索相关
    隐藏search box
    SharePoint提供的一些javascript函数
    asp.net in sharepoint
    workflow 的一点讨论
    漂亮的网站
    SwitchPartManager:在UpdatePanel中灵活地切换不同用户控件
    Serializable of an XPO Object
    树形数据的处理
    CSS中的expression
  • 原文地址:https://www.cnblogs.com/henserlinda/p/4720971.html
Copyright © 2011-2022 走看看