Shortest Prefixes(字典树)

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

解题思路

这题字典树用指针做的,看的userluoxuan的博客,嘻嘻嘻,发现指针真是个神奇的东西,还有结构体;

结构体中包含下一个节点的指针,详情请见代码


AC代码

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;

const int maxn = 26;
char word[1005][30];
struct Trie
{
    int n;
    Trie *next[maxn];
};
Trie *root;                                            //定义结构体指针,最上面的根节点

void init()
{
    root = (Trie *)malloc(sizeof(Trie));               //初始化,malloc是分配空间的
    for(int i = 0; i < maxn; i++)
        root -> next[i] = NULL;                        //根节点的26个分指针,初始化为空
}

void insert(char *word)
{
    Trie *temp = root;
    for(int i = 0; i < strlen(word); i++)
    {
        int pos = word[i] - 'a';
        if(temp -> next[pos] == NULL)                   //如果代表这条路径的指针为空
        {
            Trie *cur = (Trie *)malloc(sizeof(Trie));   //定义需新建的指针
            cur -> n = 1;                               //n记录经过的次数
            for(int j = 0; j < maxn; j++)
                cur -> next[j] = NULL;
            temp -> next[pos] = cur;
        }
        else
            temp -> next[pos] -> n++;                   //如果这条路径已存在,n自增
        temp = temp -> next[pos];                       //temp指针向下传递

    }
}

void search(char *word)
{
    Trie *temp = root;
    char ans[25];
    for(int i = 0; i < strlen(word); i++)
    {
        temp = temp -> next[ word[i] - 'a' ];
        ans[i] = word[i];
        ans[i + 1] = '\0';
        if(temp -> n == 1)                             //到只经过一次的独有字母能够做前缀
        {
            printf("%s %s\n", word, ans);
            return ;
        }
    }
    printf("%s %s\n", word, ans);                      //或者整个单词做前缀
    return ;
}


int main()
{
    int count = 0;
    init();
    while(scanf("%s", word[++count]) != EOF)           //输入EOF后count已经自增了,所以下面是 < 号
        insert(word[count]);
    for(int i = 1; i < count; i++)
        search(word[i]);
    return 0;
}


未AC代码

#include <cstdio>
#include <cstring>
#include <cstdlib>
int trie[1010][27], val[1010];
int num_jd;

void init()
{
    num_jd = 1;
    memset(trie, 0, sizeof(trie));
    memset(val, 0, sizeof(val));
}

void insert(char *s)
{
    int u = 0, i, c, len = strlen(s);
    for(i = 0; i < len; i++)
    {
        c = s[i] - 'a';
        if(!trie[u][c])
            trie[u][c] = num_jd++;
        u = trie[u][c];
        val[u]++;
    }

}

void query(char *s)
{
    int u = 0, i, j, c, len = strlen(s);
    for(i = 0; i < len; i++)
    {
        c = s[i] - 'a';
        u = trie[u][c];
        if(val[u] == 1 || i == len - 1)
        {
            printf("%s ", s);
            s[i + 1] = '\0';
            printf("%s\n", s);
            return ;
        }
    }
}

int main()
{
    char str[1010][30];
    int h, i, num_str;
    init();
    for(h = 0; ; h++)
    {
        if(scanf("%s", str[h]) != EOF)
            insert(str[h]);
        else
            break;
    }
    num_str = h;
    for(i = 0; i < num_str; i++)
    {
        query(str[i]);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值