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. python第三方库scrapy框架的安装

    1.确认python和pip安装成功 2.安装win32py          提供win32api,下载地址:https://sourceforge.net/projects/pywin32/fil ...

  2. 46. Permutations C++回溯法

    基本的回溯法 注意每次回溯回来要把上次push_back()进去的数字pop掉! class Solution { public: void backTrack(vector<int> n ...

  3. 【LeetCode】Valid Parentheses合法括号

    给定一个仅包含 '('.')'.'{'.'}'.'['.']'的字符串,确定输入的字符串是否合法. e.g. "()"."()[]{}"."[()]( ...

  4. CSS知识点(一)

    一.引入CSS样式(重点掌握) 行内样式 内接样式 外接样式 3.1 链接式 3.1 导入式 css介绍 现在的互联网前端分三层: HTML:超文本标记语言.从语义的角度描述页面结构. CSS:层叠样 ...

  5. spring context 继承

    <web-app> <display-name>Archetype Created Web Application</display-name> <conte ...

  6. ActiveMQ producer同步/异步发送消息

    http://activemq.apache.org/async-sends.html producer发送消息有同步和异步两种模式,可以通过代码配置: ((ActiveMQConnection)co ...

  7. WebSphere安装教程(WAS6.1为例)

    1.网络准备 我们选择图形界面安装,如果堡垒机是windows则要在目标机器安装桌面环境并开启vcnserver:如果堡垒机是Linux则在堡垒机安装桌面环境和vncserver,然后将目标机的DIS ...

  8. 把旧系统迁移到.Net Core 2.0 日记 (12) --发布遇到的问题

    1. 开发时是在Mac+MySql, 尝试发布时是在SQL2005+Win 2008 (第一版) 在Startup.cs里,数据库连接要改,分页时netcore默认是用offset关键字分页, 如果用 ...

  9. nyoj 0325 zb的生日(dp)

    nyoj 0325 zb的生日 zb的生日 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集 ...

  10. 【Loadrunner_WebService接口】对项目中的GetProduct接口生成性能脚本

    一.环境 https://xxx.xxx.svc?wsdl 用户名:username 密码:password 对其中的GetProduct接口进行测试 备注:GetProducts.xml文件内容和S ...