POJ 2001 Shortest Prefixes 数据结构Trie树(字典树、前缀树)

Shortest Prefixes
Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%lld & %llu
Submit
Status

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


解题思路:
这题给了这么多的模式串,基本确定就是字典树的题目了,大体的框架就在这,只要稍稍的修改即可
首先输入字符串建树,节点数目为1000*20*26足够,那就是普通的建树过程
找前缀就是在字典树里沿着它的路径走,确定为前缀的策略根据题目描述
缩写要求为:
1,完整的单词
2,不产生和其他单词有一样的前缀
对应在字典树里的策略就是:
1,走到这个单词的单词节点
2,沿着这个单词在字典树上走一旦发现这个路径上只有自己存在,就确定为前缀
在字典树里添加一个记录每一个节点后面还有的节点的数量的数组,数量为1就输出。


///给一堆单词,获得每一个单词的最佳缩写
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxnode = 1000 * 30;
const int sigma_size = 30;
char data[1005][30] ;
struct Trie {
    int ch[maxnode][sigma_size];
    int val[maxnode];
    int num[maxnode];
    int sz;
    void clear(){
        sz = 1;
        memset(ch[0], 0, sizeof(ch[0]));
        memset(num, 0, sizeof(num));
    }
    int idx(char c) { return c - 'a'; }
    void insert(const char *s, int v) {
        int u = 0, n = strlen(s);
        for(int i = 0; i < n; i++) {
            int c = idx(s[i]);
            if(!ch[u][c]) {
                memset(ch[sz], 0, sizeof(ch[sz]));
                val[sz] = 0;
                num[sz] = 0 ;
                ch[u][c] = sz++;
            }

            u = ch[u][c];
            num[u]++ ;
        }
        val[u] = v;
//        for(int i=0;i<=sz;i++){
//            printf("%d ",num[i]);
//        }printf("\n");
    }
    void find(char *s,int x){
        int u = 0;
        int p = 0 ;
        for(int i = 0; i < strlen(s); i++) {
            //printf("i = %d\n",i);
            if(s[i] == '\0'||num[u]==1){
                //printf("num[%d] = %d\n",u,num[u]);
                break;
            }
            int c = idx(s[i]);
            u = ch[u][c];
            p++ ;
        }
        for(int i=0;i<p;i++){
            printf("%c",s[i]);
        }printf("\n");
    }
};
char str[maxnode] ;
Trie trie;
int main() {
    int c = 1 ;
    trie.clear();
    while(~scanf("%s",str)){
        trie.insert(str, c);
        strcpy(data[c++],str) ;

    }

//    for(int i=0;i<=sz;i++){
//        printf("%d ",num[i]);
//    }printf("\n");

    for(int i=1;i<=c;i++){
        printf("%s ",data[i]);
        trie.find(data[i],i);
    }
    //printf("\n");
    return 0;
}

    原文作者:Trie树
    原文地址: https://blog.csdn.net/fans_ac/article/details/52004711
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞