Shortest Prefixes POJ - 2001(字典树)

本文介绍了一种用于找出每个单词最短且能唯一标识该单词的前缀的算法,并通过字典树实现。该算法首先将所有单词构建成字典树结构,再遍历每个单词,查找其最短唯一前缀。

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

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
思路:把所给的字符串都放进字典树,记录每个前缀出现的次数,然后对于每个字符串进行一个字符一个字符从头查找,当查找到这个前缀记录次数为1的时候,说明这个前缀只有
这个字符串有,这个前缀即为所求前缀
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
char s[1009][35];//存放字符串
struct TNode{
    int time;
    struct TNode* Next[26];//字典树结构体
}tre;
typedef struct TNode* Tree;
void Insert(char a[]){//字典树插入
    int i,j;
    Tree p = &tre;
    Tree q;
    for(i = 0; a[i]; i++){
        if(p->Next[a[i]-'a']==NULL){
            q = (Tree)malloc(sizeof(struct TNode));
            q->time = 0;
            for(j = 0; j < 26; j++)q->Next[j] = NULL;
            p->Next[a[i]-'a'] = q;
        }
        p = p->Next[a[i]-'a'];
        p->time++;
    }
}
void Find(char a[]){
    int i,j;
    Tree p = &tre;
    for(i = 0; a[i]; i++){
        printf("%c",a[i]);//每次输出一个字符
        p = p->Next[a[i]-'a'];
        if(p->time==1){//判断这个字符,以这个字符为末尾的前缀出现次数是否为1,是1就终止,当前输出的即所求前缀
            return;
        }
    }
}
int main(){
    int i;
    int cnt = 0;
    for(i = 0; i < 26; i++)tre.Next[i] = NULL;//初始化头节点
    while(~scanf("%s",s[cnt++]));
    for(i = 0; i < cnt; i++){
        Insert(s[i]);//输入
    }
    for(i = 0; i < cnt; i++){
        printf("%s ",s[i]);
        Find(s[i]);//查找
        puts("");
    }
    return 0;
}

 
GCN (Graph Convolutional Network) Shortest-Path-Master 是一种基于图卷积网络的最短路径算法。最短路径问题是图论中的经典问题,对于给定的图和起始点,找到到达目标点的最短路径。 GCN Shortest-Path-Master 通过应用图卷积神经网络的思想来解决最短路径问题。传统的最短路径算法(如Dijkstra算法或贝尔曼-福特算法)在计算过程中不考虑节点的特征信息,只利用图的拓扑结构。而GCN Shortest-Path-Master 利用了节点的特征信息,将节点的邻居节点信息通过图卷积操作进行聚合,得到节点的新特征表示。 GCN Shortest-Path-Master 的核心思想是,通过图卷积层不断更新节点的特征表示,使得节点的特征表示能够包含更多关于最短路径的信息。在每次迭代中,GCN Shortest-Path-Master 将节点的特征与邻居节点的特征进行聚合,得到节点的新特征表示。在网络的最后一层,通过对所有节点进行分类任务,可以得到每个节点到达目标点的最短路径预测。 相比传统的最短路径算法,GCN Shortest-Path-Master 提供了以下优势: 1. GCN Shortest-Path-Master 能够利用节点的特征,从而更好地表达节点之间的相互作用和联系。 2. GCN Shortest-Path-Master 可以自适应地学习节点的特征表示,而无需人工定义特征。 3. GCN Shortest-Path-Master 可以处理大规模的图结构,在计算效率上具有一定优势。 总之,GCN Shortest-Path-Master 是一种基于图卷积神经网络的最短路径算法,通过利用节点的特征信息,能够更好地解决最短路径问题。它在图结构数据中的应用具有很大潜力,在社交网络分析、推荐系统和物流路径规划等领域都有广泛的应用前景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值