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

来源:互联网 发布:mac标点 编辑:程序博客网 时间:2024/05/17 08:05
Shortest Prefixes
Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%lld & %llu
SubmitStatus

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

carbohydratecartcarburetorcaramelcariboucarboniccartilagecarboncarriagecartoncarcarbonate

Sample Output

carbohydrate carbohcart cartcarburetor carbucaramel caracaribou caricarbonic carbonicartilage carticarbon carboncarriage carrcarton cartocar carcarbonate 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;}



0 0