zoukankan      html  css  js  c++  java
  • POJ2001 Shortest Prefixes[字典树判断唯一前缀]

    Shortest Prefixes
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 9754   Accepted: 4130

    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

     
     
     
     
     
     
    字典树判断字符的唯一性
     
     
     
    code:
     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 char str[10100][22];
     6 
     7 struct node
     8 {
     9     int cnt;
    10     node *next[26];
    11 }*p;
    12 
    13 void build(char str[],int k,node *head)
    14 {
    15     while(k<strlen(str))
    16     {
    17         if(head->next[str[k]-'a']!=NULL)
    18         {
    19             head->next[str[k]-'a']->cnt+=1;
    20             head=head->next[str[k]-'a'];
    21         }
    22         else
    23         {
    24             head->next[str[k]-'a']=new node;
    25             head=head->next[str[k]-'a'];
    26             head->cnt=1;
    27             for(int i=0;i<26;i++)
    28                 head->next[i]=NULL;
    29         }
    30         k++;
    31     }
    32 }
    33 
    34 void search(char str[],int k,node *head)
    35 {
    36     while(k<strlen(str))
    37     {
    38         if(head->next[str[k]-'a']!=NULL)
    39         {
    40             printf("%c",str[k]);
    41             if(head->next[str[k]-'a']->cnt==1)
    42                 return;
    43         }
    44         head=head->next[str[k]-'a'];
    45         k++;
    46     }
    47 }
    48 
    49 void del(node *head)
    50 {
    51     if(head==NULL)
    52         return;
    53     for(int i=0;i<26;i++)
    54     {
    55         del(head->next[i]);
    56         head->next[i]=NULL;
    57     }
    58     delete(head);
    59     return;
    60 }
    61 
    62 int main()
    63 {
    64     p=new node;
    65     for(int i=0;i<26;i++)
    66         p->next[i]=NULL;
    67     int cnt=0;
    68     while(~scanf("%s",str[cnt]))
    69     {
    70         build(str[cnt],0,p);
    71         cnt++;
    72     }
    73     for(int i=0;i<cnt;i++)
    74     {
    75         printf("%s ",str[i]);
    76         search(str[i],0,p);
    77         printf("\n");
    78     }
    79     del(p);
    80     return 0;
    81 }
  • 相关阅读:
    vue前台 (三)优化三级分类请求次数(发送ajax次数)
    vue前台(三)采用申明式导航去点击连接到其他组件(采用事件委派方式)
    浅谈Vue中的路由配置项meta
    vue前台(三)函数防抖和节流,lodash
    vuex的mapState 辅助函数
    Vue使用NProgress
    axios拦截器
    编程式路由跳转到当前路由, 控制台抛出NavigationDuplicated的错误
    window 右键菜单添加 vscode
    对《Python核心编程》中“第一个Python程序”的改进
  • 原文地址:https://www.cnblogs.com/XBWer/p/2676875.html
Copyright © 2011-2022 走看看