• Shortest Prefixes(trie树唯一标识)


    Shortest Prefixes
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 15948   Accepted: 6887

    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
    题解:让找唯一能确定一个单词的前缀;我们想到当唯一确定的时候必定word[k]是1;那么很好解决了
    代码:
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<vector>
     7 #define mem(x,y) memset(x,y,sizeof(x))
     8 using namespace std;
     9 typedef long long LL;
    10 const int MAXN=10100;
    11 char s[1010][30];
    12 int ch[MAXN][30],word[MAXN];
    13 int sz;
    14 void initial(){
    15     sz=1;
    16     mem(word,0);
    17 }
    18 void insert(char *s){
    19     int len=strlen(s);
    20     int j,k=0;
    21     for(int i=0;i<len;i++){
    22         j=s[i]-'a';
    23         if(!ch[k][j]){
    24             mem(ch[sz],0);
    25             ch[k][j]=sz++;
    26         }
    27         k=ch[k][j];
    28         word[k]++;
    29     }
    30 }
    31 void find(char *s){
    32     int len=strlen(s);
    33     int j,k=0;
    34     for(int i=0;i<len;i++){
    35         j=s[i]-'a';
    36         k=ch[k][j];
    37         printf("%c",s[i]);
    38         if(word[k]==1)return;
    39     }
    40 }
    41 int main(){
    42     int x=0;
    43     initial();
    44     while(~scanf("%s",s[x])){
    45         insert(s[x]);x++;
    46     }
    47     for(int i=0;i<x;i++){
    48         printf("%s ",s[i]);
    49         find(s[i]);puts("");
    50     }
    51     return 0;
    52 }

  • 相关阅读:
    drawable转mitmap 以及图片base64编码
    接口传值实例DatePickerDialog
    android showDialog用法
    andorid ListView和GirdView 与ScrollView 冲突
    public boolean onKeyDown(int keyCode, KeyEvent event)
    android PopupWindow
    android 异步线程刷新UI 以及 JSON解析 以及 url get请求
    2020-08-06:现有一批邮件需要发送给订阅顾客,且有一个集群(集群的节点数不定,会动态扩容缩容)来 负责具体的邮件发送任务,如何让系统尽快地完成发送? 请详述技术方案!
    2020-08-05:请解释下为什么鹿晗发布恋情的时候, 微博系统会崩溃,如何解决?
    2020-08-04:简单工厂、工厂方法和抽象工厂的区别是什么?
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4909406.html
Copyright © 2020-2023  润新知