• POJ 2001 Shortest Prefixes


    Shortest Prefixespoj2001
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 18684   Accepted: 8086

    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

     
    裸的trie。
    设置一个访问数组。
    每次枚举就好
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<queue>
     6 #include<algorithm>
     7 #include<cstdlib>
     8 using namespace std;
     9 const int MAXN=100001;
    10 void read(int &n)
    11 {
    12     char c='+';int x=0,flag=1;
    13     while(c<'0'||c>'9')
    14     {c=getchar();if(c=='-')flag=-1;}
    15     while(c>='0'&&c<='9')
    16     {x=x*10+c-48;c=getchar();}
    17     n=(x*flag);
    18 }
    19 struct TRIE
    20 {
    21     int ch[MAXN][51];
    22     int sz;
    23     int val[MAXN];
    24     TRIE()
    25     {memset(ch[0],0,sizeof(ch[0]));
    26     memset(val,0,sizeof(val));
    27     sz=1;}
    28     int idx(char c)
    29     {
    30         return c-'a';
    31     }    
    32     void Insert(char *s)
    33     {
    34         int l=strlen(s);int now=0;
    35         for(int i=0;i<l;i++)
    36         {
    37             char c=idx(s[i]);
    38             if(!ch[now][c])
    39             {
    40                 memset(ch[sz],0,sizeof(ch[sz]));
    41                 ch[now][c]=sz++;
    42             }
    43             now=ch[now][c];
    44             val[now]++;
    45             //printf("%d ",val[now]);
    46         }
    47     }
    48     void query(char *s)
    49     {
    50         int l=strlen(s);
    51         int now=0;
    52         for(int i=0;i<l;i++)
    53         {
    54             int c=idx(s[i]);
    55             now=ch[now][c];
    56             printf("%c",s[i]);    
    57             if(val[now]==1)
    58             return ;
    59         }
    60     }
    61 }trie;
    62 char s[MAXN][201];
    63 int n=1;
    64 
    65 int main()
    66 {
    67     
    68     while(scanf("%s",s[n])==1)
    69     {
    70         n++;
    71         trie.Insert(s[n-1]);
    72     }
    73     for(int i=1;i<n;i++)
    74     {
    75         printf("%s ",s[i]);
    76         trie.query(s[i]);
    77         printf("
    ");
    78     }
    79     return 0;
    80 } 
  • 相关阅读:
    Central Contract无法传到ECC的排查工具
    直接采购在Core SRM与SRM PPS间的区别
    整理一下思路
    PR无法修改的几个原因
    MARCTRAME与MARCHTRAME
    Revision Level在PS中的决定
    Storage Location在物料分配时的选择逻辑
    如何正确删除WBS下的Planned Independent Requirement
    Open XML SDK 2.0
    Silverlight使用问题汇总一
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/7123335.html
Copyright © 2020-2023  润新知