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 题意:求每个字符串最短的与别的字符不相同的前缀
 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std; struct trie
{
trie* next[];//下一个结点
int num;//以当前字符串为前缀的数量
trie()//构造函数
{
int i;
for(i=;i<;i++)
next[i]=NULL;
num=;
}
}; trie root; void insert(char word[])
{
trie* r=&root;
int i;
for(i=;word[i];i++)
{
if(r->next[word[i]-'a']==NULL)//这个字符没有
r->next[word[i]-'a']= new trie;
r=r->next[word[i]-'a'];
r->num++;
}
} void find(char word[])
{
trie* r=&root;
int i;
for(i=;word[i];i++)
{
printf("%c",word[i]);
if(r->next[word[i]-'a']==NULL)
return;
r=r->next[word[i]-'a'];
if(r->num==)
return;
}
return ;
} int main()
{
char word[][];
int i=;
while(gets(word[i]))
{
if(word[i][]==NULL)
break;
insert(word[i]);
i++;
}
for(int ii=;ii<i;ii++)
{
printf("%s ",word[ii]);
find(word[ii]);
printf("\n");
}
return ;
}



POJ 2001 Shortest Prefixes(字典树)的更多相关文章

  1. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  2. poj 2001 Shortest Prefixes(字典树trie 动态分配内存)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15610   Accepted: 673 ...

  3. poj 2001 Shortest Prefixes(字典树)

    题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...

  4. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  5. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  6. POJ 2001 Shortest Prefixes(字典树活用)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21651   Accepted: 927 ...

  7. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  8. POJ 2001 Shortest Prefixes (Trie)

    题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...

  9. poj 2001 Shortest Prefixes trie入门

    Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...

  10. poj2001 Shortest Prefixes(字典树)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21642   Accepted: 926 ...

随机推荐

  1. leetcode-algorithms-32 Longest Valid Parentheses

    leetcode-algorithms-32 Longest Valid Parentheses Given a string containing just the characters '(' a ...

  2. PAT 1008 Elevator

    1008 Elevator (20 分)   The highest building in our city has only one elevator. A request list is mad ...

  3. 函数使用十一:FTP

    网上很多FTP说明的文档,也很详细,写这玩意是备份一下: 注:有的系统需要配置FTP地址和端口,有的好像没有... SM30->SAPFTP_SERVERS_V,默认端口21. 在做之前可以做个 ...

  4. 浅谈mysql中各种表空间(tablespaces)的概念

    mysql中,会涉及到各种表空间的概念,虽然,很多方面这些概念和Oracle有相似性,但也有很多不同的地方,初学者很容易被这些概念弄的晕头转向,从而,混淆这些概念的区别和理解,下面,就简要介绍和说明一 ...

  5. 用SQL命令手工创建CDB

    1.设置环境变量  $ export ORACLE_SID=ora12c  $ export ORACLE_HOME=/home/oracle/product/12.1.0/db1  $ export ...

  6. PE文件结构解析

    说明:本文件中各种文件头格式截图基本都来自看雪的<加密与解密>:本文相当<加密与解密>的阅读笔记. 1.PE文件总体结构 PE文件框架结构,就是exe文件的排版结构.也就是说我 ...

  7. 查验身份证 (15 分) 一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值:

    // test4.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束.// #include "pch.h"#include <ios ...

  8. vue 给 图片添加一个默认图片

    <img v-bind:src="userData.photo" :onerror="logo" class="img-box4"&g ...

  9. 【基础】selenium中元素定位的常用方法(三)

    一.Selenium中元素定位共有八种 id name className tagName linkText partialLinkText xpath cssSelector 其中前六种都比较简单, ...

  10. Five Great .NET Framework 4.5 Features (五大特性)

    [译].Net 4.5 的五项强大新特性   本文原文:Five Great .NET Framework 4.5 Features译者:冰河魔法师 目录 介绍 特性一:async和await 特性二 ...