Shortest Prefixes POJ - 2001 (字典树模板题~)

本文介绍了一种使用字典树(Trie)来解决字符串集合中寻找每个单词最短唯一前缀的问题。通过构建字典树并利用其结构特性,文章详细解释了插入和搜索算法,并给出具体实例。

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

字典树模板题~ 用了静态数组的方法

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

struct Trie {
    int value;
    int child[26]; // 子节点最多有26个(小写字母或大写字母)
    Trie() {  //构造函数
        value = 0;
        memset(child, 0, sizeof(child));
    }
}trie[3100];

int trieN = 0;

void insert(char str[]) {
    int x = 0;
    for (int i = 0; str[i]; i++) {
        int d = str[i] - 'a';
        if (trie[x].child[d] == NULL) {  // 如果该节点没有子树
            trie[++trieN] = Trie();
            trie[x].child[d] = trieN;  
        }
        x = trie[x].child[d];
        trie[x].value++;
    }
    
}

void search(char str[]) {
    int x = 0, len = strlen(str);
    for (int i = 0; i < len; i++) {
        if (trie[x].value == 1) break;
        int d = str[i] - 'a';
        cout << str[i];
        //cout << trie[x].value;
        x = trie[x].child[d];
    }
}

int main(void) {
    //freopen("in.txt", "r", stdin);
    char s[3010][26];
    int cnt = 0;
    while (~scanf("%s", s[cnt])) {
        //cout << s[cnt] << endl;
        insert(s[cnt]);
        cnt++;
    }
    for (int i = 0; i < cnt; i++) {
        printf("%s ", s[i]);
        search(s[i]);
        cout << endl;
    }
    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 是一种基于图卷积神经网络的最短路径算法,通过利用节点的特征信息,能够更好地解决最短路径问题。它在图结构数据中的应用具有很大潜力,在社交网络分析、推荐系统和物流路径规划等领域都有广泛的应用前景。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值