zoukankan      html  css  js  c++  java
  • POJ2001 (Shortest Prefixes)

    Shortest Prefixes
    Time Limit: 1000MS   Memory Limit: 30000K
         

    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<set>
     2 #include<map>
     3 #include<queue>
     4 #include<cstdio>
     5 #include<cstdlib>
     6 #include<cstring>
     7 #include<iostream>
     8 #include<algorithm>
     9 using namespace std;
    10 const int N = 30;
    11 const int MAXN = 1010;
    12 #define For(i,n) for(int i=1;i<=n;i++)
    13 #define Rep(i,l,r) for(int i=l;i<=r;i++)
    14 
    15 char word[MAXN][N],ans[N];
    16 int n;
    17 
    18 struct trie{
    19     int sz,ch[MAXN*N][N],v[MAXN*N];
    20     trie(){sz=1;}
    21     void insert(char st[]){
    22         int len=strlen(st);int now=1;v[now]++;
    23         Rep(i,0,len-1){
    24             int id=st[i]-'a';
    25             if(!ch[now][id]) ch[now][id]=++sz;
    26             v[now=ch[now][id]]++;
    27         }
    28     }
    29     void prefix(char st[],char *ans){
    30         int now=1,len=strlen(st);
    31         Rep(i,0,len-1){
    32             int id=st[i]-'a';
    33             if(v[now]==1){ans[i]='';return;}
    34             ans[i]=st[i];
    35             now=ch[now][id];
    36         }
    37         ans[len]='';
    38     }
    39 }trie;
    40 
    41 int main(){
    42     #ifndef ONLINE_JUDGE
    43         freopen("prefix.in","r",stdin);
    44     #endif
    45     for(n=1;scanf("%s",word[n])!=EOF;n++) trie.insert(word[n]);
    46     For(i,n) {
    47         trie.prefix(word[i],ans);
    48         printf("%s %s
    ",word[i],ans);
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    手动执行把nconf生成的配置文件附加到nagios下
    创建django工程
    mysql安装、配置、导入数据库
    PHP里<?php和<?通用要在配置里怎么设置_百度知道
    Loganalyzer数据库乱码解决方法:
    RSyslog安装配置
    修改cacti的系统时间
    Mysql5.7.11 安装 cacti0.8.8f ,在导入cacti.sql数据库时出现下记错误,导致数据库导入终止: ERROR 1067 (42000) at line 1847: Invalid default value for 'status_fail_date'
    中间件组件
    cookie与session 组件
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3967904.html
Copyright © 2011-2022 走看看