zoukankan      html  css  js  c++  java
  • Shortest Prefixes

    Shortest Prefixes
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 17715   Accepted: 7698

    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

    /*
    每插入一个单词,把经过的路径都在v数组中加一
    遍历的时候分割单词,如果某一个前缀的最后一个字母只被标记过一次,那么这个前缀就是是能用的
    */
    #include<iostream>
    #include<stdio.h>
    #include<string>
    #include<string.h>
    #define MAX 26
    #define N 25
    #define M 1005
    using namespace std;
    const int maxnode=4000*100+100;///预计字典树最大节点数目
    const int sigma_size=26;///每个节点的最多儿子数
    
    struct Trie
    {
        ///这里ch用vector<26元素的数组> ch;实现的话,可以做到动态内存
        int ch[maxnode][sigma_size];///ch[i][j]==k表示第i个节点的第j个儿子是节点k
        int val[maxnode];///val[i]==x表示第i个节点的权值为x
        int sz;///字典树一共有sz个节点,从0到sz-1标号
        int v[maxnode];
    
        ///初始化
        void Clear()
        {
            sz=1;
            memset(ch[0],0,sizeof(ch[0]));///ch值为0表示没有儿子
            memset(v,0,sizeof v);
        }
    
        ///返回字符c应该对应的儿子编号
        int idx(char c)
        {
            return c-'a';
        }
    
        ///在字典树中插入单词s,但是如果已经存在s单词会重复插入且覆盖权值
        ///所以执行Insert前需要判断一下是否已经存在s单词了
        void Insert(char *s)
        {
            int u=0,n=strlen(s);
            for(int i=0;i<n;i++)
            {
                int id=idx(s[i]);
                if(ch[u][id]==0)///无该儿子
                {
                    ch[u][id]=sz;
                    memset(ch[sz],0,sizeof(ch[sz]));
                    val[sz++]=0;
                }
                u=ch[u][id];
                v[u]++;
            }
            val[u]=n;
        }
    
        ///在字典树中查找单词s
        int Search(char *s)
        {
            int n=strlen(s),u=0;
            for(int i=0;i<n;i++)
            {
                int id=idx(s[i]);
                if(ch[u][id]==0)
                    return -1;
                u=ch[u][id];
            }
            return v[u];
        }
    };
    char op[M][N];
    Trie trie;///定义一个字典树
    int main()
    {
        //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
        trie.Clear();
        int len=0;
        while(gets(op[len++]))
        {
            trie.Insert(op[len-1]);
            //cout<<op[len-1]<<endl;
        }
        for(int i=0;i<len-1;i++)
        {
            printf("%s ",op[i]);
            int f=1;
            int n=strlen(op[i]);
            //cout<<"n="<<n<<endl;
            for(int k=1;k<=n;k++)
            {
                char s1[N];
                strncpy(s1,op[i],n);
                s1[k]='';
                //cout<<s1<<endl;
                if(trie.Search(s1)==1)
                {
                    f=0;
                    puts(s1);
                    break;
                }
            }
            if(f)
                puts(op[i]);
        }
        return 0;
    }
  • 相关阅读:
    js中图片上传,多次上传同一张不生效
    mysql统计一年12月的数据
    jquery.qrcode.js 生成二维码并支持中文的方法
    layui 的 GitHub 及 Gitee (码云) 仓库
    枚举应用demo
    第十二节 线性回归:波士顿房价预测
    第十二节 gevent多任务
    第十一节 yield实现多任务
    第十节 next和send取生成器的值
    第九节 生成器
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6008582.html
Copyright © 2011-2022 走看看