zoukankan      html  css  js  c++  java
  • 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 }

  • 相关阅读:
    Oracle如何定义两个数组变量
    Oracle 数组定义
    Oracle的Number对应C#数据类型
    Oracle删除临时表
    我的第一个Flutter 项目(电商)
    Dart Mac 安装环境(无敌)
    React Native 问题(运行)
    TS的一些用法和普通的对比(Vue)
    vue2.0和vue3.0的响应式原理
    简易orm 主要是为了旧平台查询方便
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4909406.html
Copyright © 2011-2022 走看看