Shortest Prefixes(POJ-2001)

Problem 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

题意:给出多个单词,现在要输出每个单词的前缀使得每个前缀唯一,若不存在唯一的前缀就直接输出单词本身

思路:

首先用所有单词建立字典树,并建一个数组 val 存储当前结点为根的子树下的单词个数

然后对于每个单词查找一遍字典树,如果一个结点的 val=1,那么就直接输出该结点并返回,否则输出当前完整单词

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define E 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD=7;
const int N=50000+5;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;

int tot;
int trie[N][26];//trie[rt][x]=tot,root是上个节点编号,x是字母,tot是下个节点编号
bool vis[N];//查询整个单词用
int val[N];
void insert(char *s,int root){
    int len=strlen(s);
    for(int i=0;i<len;i++){
        int x=s[i]-'a';
        if(trie[root][x]==0){//现在插入的字母在之前同一节点处未出现过
            trie[root][x]=tot;//字母插入一个新的位置,否则不做处理
            val[tot]=0;//记录以当前结点为根的子树下单词个数
            tot++;
        }
        root=trie[root][x];//为下个字母的插入做准备
        val[root]++;
    }
}
void find(char *s,int root){
    int len=strlen(s);
    for(int i=0;i<len;i++){
        int x=s[i]-'a';
        root=trie[root][x];//为查询下个字母做准备
        printf("%c",s[i]);//输出当前字母
        if(val[root]==1)//为1时直接返回
            return;
    }
}
char word[N][50];
int main(){
    int cnt=0;
    tot=1;
    while(scanf("%s",word[cnt])!=EOF)
        insert(word[cnt++],0);
    for(int i=0;i<cnt;i++){//枚举所有单词
        printf("%s ",word[i]);
        find(word[i],0);
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值