poj 2001 Shortest Prefixes(特里)

本文介绍如何通过Trie树解决POJ2001问题,即为每个输入的单词找到最短且唯一的前缀,以区分列表中的其他单词。通过构造Trie树并遍历每个单词,程序可以有效地找到所需的前缀。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主题链接:http://poj.org/problem?id=2001


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


题意:

寻找在单词表中能唯一作为此单词的缩写!


代码例如以下:

#include <cstdio>
#include <cstring>
#include <malloc.h>
#include <iostream>
using namespace std;
#define MAXN 26
char str[1017][MAXN];
typedef struct Trie
{
    int v;//依据须要变化
    Trie *next[MAXN];
    //next是表示每层有多少种类的数。假设仅仅是小写字母。则26就可以,
    //若改为大写和小写字母,则是52,若再加上数字,则是62了
} Trie;
Trie* root;

void createTrie(char *str)
{
    int len = strlen(str);
    Trie *p = root, *q;
    for(int i = 0; i < len; i++)
    {
        int id = str[i]-'a';
        if(p->next[id] == NULL)
        {
            q = (Trie *)malloc(sizeof(Trie));
            q->v = 1;//初始v==1
            for(int j = 0; j < MAXN; j++)
                q->next[j] = NULL;
            p->next[id] = q;
            p = p->next[id];
        }
        else
        {
            p->next[id]->v++;
            p = p->next[id];
        }
    }
    // p->v = -1;//若为结尾。则将v改成-1表示
}

void findTrie(char *str)
{
    char ss[MAXN];
    int len = strlen(str);
    Trie *p = root;
    for(int i = 0; i < len; i++)
    {
        int id = str[i]-'a';
        p = p->next[id];
        ss[i] = str[i];
        ss[i+1] = '\0';
        if(p->v == 1) //能够唯一标示该字符串的前缀
        {
            printf("%s %s\n",str,ss);
            return ;
        }
        //   return 0;
        //  if(p->v == -1)   //字符集中已有串是此串的前缀
        //      return -1;
    }
    //return p->v;
    // return -1;   //此串是字符集中某串的前缀
    printf("%s %s\n",str,str);
    // return;
}
int main()
{
    int cont = 0;
    root = (Trie *)malloc(sizeof(Trie));
    for(int i = 0; i < MAXN; i++)
        root->next[i] = NULL;
    while(scanf("%s",str[cont])!=EOF)
    {
        createTrie(str[cont]);
        cont++;
    }
    for(int i = 0; i < cont; i++)
    {
        findTrie(str[i]);
    }
    return 0;
}


版权声明:本文博主原创文章,博客,未经同意,不得转载。

内容概要:本文档主要展示了C语言中关于字符串处理、指针操作以及动态内存分配的相关代码示例。首先介绍了如何实现键值对(“key=value”)字符串的解析,包括去除多余空格和根据键获取对应值的功能,并提供了相应的测试用例。接着演示了从给定字符串中分离出奇偶位置字符的方法,并将结果分别存储到两个不同的缓冲区中。此外,还探讨了常量(const)修饰符在变量和指针中的应用规则,解释了不同类型指针的区别及其使用场景。最后,详细讲解了如何动态分配二维字符数组,并实现了对这类数组的排序与释放操作。 适合人群:具有C语言基础的程序员或计算机科学相关专业的学生,尤其是那些希望深入理解字符串处理、指针操作以及动态内存管理机制的学习者。 使用场景及目标:①掌握如何高效地解析键值对字符串并去除其中的空白字符;②学会编写能够正确处理奇偶索引字符的函数;③理解const修饰符的作用范围及其对程序逻辑的影响;④熟悉动态分配二维字符数组的技术,并能对其进行有效的排序和清理。 阅读建议:由于本资源涉及较多底层概念和技术细节,建议读者先复习C语言基础知识,特别是指针和内存管理部分。在学习过程中,可以尝试动手编写类似的代码片段,以便更好地理解和掌握文中所介绍的各种技巧。同时,注意观察代码注释,它们对于理解复杂逻辑非常有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值