Shortest Prefixes
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16617   Accepted: 7220

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

 
转:

首次认识到Trie树的强大之处!简单易懂,只要对建立一般的树的方法有所了解就OK了。Trie树,又称单词查找树键树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。

它有3个基本特性:

 

  1)根节点不包含字符,除根节点外每一个节点都只包含一个字符。

 

  2)从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。

 

  3)每个节点的所有子节点包含的字符都不相同。

只需要统计每个字母出现个数就O 了;
//魔性的代码
#include <cstdio>
#include <cstring>
#define M 200005
struct trie_type
{
    int cnt, next[30];
}t[M];
int n=0, tot=0;
char s[1005][30];
void insert(char *ch)
{
    int p=0, l=strlen(ch);
    for(int i=0; i<=l-1; i++)
    {
        int tmp=ch[i]-'a';
        if(!t[p].next[tmp]) t[p].next[tmp]=++tot;
        p=t[p].next[tmp];
        t[p].cnt++;
            //printf("%d %c %d\n", p, ch[i], t[p].cnt);
    }
    //printf("%d\n", p);
}
void find(char *ch)
{
    int p=0, l=strlen(ch);
    printf("%s ", ch);
    for(int i=0; i<l; i++)
    {
        printf("%c", ch[i]);
        int tmp=ch[i]-'a';
        p=t[p].next[tmp];
        if(t[p].cnt==1) break;
    }
    printf("\n");
}
int main()
{
    while(scanf("%s", s[++n]) != EOF) insert(s[n]);
    for(int i=1; i<=n-1; i++)
        find(s[i]);
    return 0;
}

/**

*/

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int alpher = 26;
struct Trinode 
{
    int num;
    Trinode *next[alpher];
    Trinode()
    {
        num=0;
        memset(next, 0, sizeof(next));
    }
};
Trinode *root=new Trinode;

void Insert(char *str)
{
    int i=0; 
    Trinode *temp=root;
    while(str[i])
    {
        int nextpos=str[i]-'a';
        if(temp->next[nextpos]==0)
        temp->next[nextpos]=new Trinode;
        temp=temp->next[nextpos];
        temp->num++;
        i++;
    }
}

void Search(char *str)
{
    int i = 0;
    Trinode*temp = root;
    while(str[i])
    {
        int nextPos = str[i]-'a';
        if(temp->num==1)
        return;
        cout<<str[i];
        temp = temp->next[nextPos];
        i++;
    }
}

int main()
{
    char str[1001][22];
    int cnt=0;
    while(scanf("%s", str[cnt]) != EOF)
    {
        Insert(str[cnt]);
        cnt++;
    }
    for(int i=0; i<cnt; i++)
    {
        cout<<str[i]<<" ";
        Search(str[i]);
        cout<<endl;
    }
    return 0;
}

 

posted on 2016-03-20 21:15  cleverbiger  阅读(172)  评论(0)    收藏  举报