Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350677
  • 博文数量: 122
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 1191
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 11:12
文章分类

全部博文(122)

文章存档

2010年(122)

我的朋友

分类: C/C++

2010-05-07 00:04:50

一、问题描述

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

 

二、解题思路

    使用trie树结构。在trie树节点中加入两个域iswordcntisword分别表示从根节点到该节点是否是一个单词,cnt表示有多少个单词经过这个节点。先将所有单词保存在trie树中,然后一个一个地查找,当到达某个节点使用cnt==1 或者 isword==true,那么从根到该节点组成的字符串便是该单词的最短前缀。

三、代码

 

#include<iostream>
using namespace std;
struct Node
{
    int cnt;
    int next[26];
    bool isword;
    Node()
    {
        cnt=0;
        isword=false;
        memset(next,-1,sizeof(next));
    }
};
Node T[30005];
char words[1005][25];
int len=1;//T数组下标
char pre[30];
int idx=0;
void insert(char * tar)
{
    Node * p=& T[0];
    int mid;
    while(*tar)
    {
        p->cnt++;
        mid=*tar-'a';
        if(p->next[mid]==-1)
        {
            p->next[mid]=len;
            len++;
        }
        p=&T[ p->next[mid] ];
        tar++;
    }
    p->cnt++;
    p->isword=true;
}
void search(char * tar)
{
    Node * p =& T[0];
    int id;
    while(p->cnt >1 && *tar)
    {
        pre[idx++]=*tar;
        id=*tar-'a';
        p=&T[ p->next[id] ];
        tar++;
    }
    pre[idx]='\0';
}
int main()
{
    int i,j;
    i=0;
    len=1;
    while(scanf("%s",&words[i])!=EOF)
    {
        char temp[25];
        strcpy(temp,words[i]);
        insert(temp);
        i++;
    }
    for(j=0;j<i;++j)
    {
        char t[25];
        strcpy(t,words[j]);
        idx=0;
        search(t);
        printf("%s %s\n",words[j],pre);
    }
    return 0;
}


阅读(2021) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~