Shortest Prefixes

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 21651   Accepted: 9277

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

题意:输出每个单词以及它的独有前缀;

分析:将输入单词建树,记录每个节点访问次数,建树完成后枚举每个单词的节点并输出节

点对应的字母,找到访问次数为1的节点则停止查询该单词

数组实现

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int t[15000][26];
char ss[1001][21];
int num[15000],pos=1;
void insert(char *s)
{
int x,rt=0;
for(int i=0;s[i];i++)
{
x=s[i]-'a';
if(!t[rt][x])
t[rt][x]=pos++;
rt=t[rt][x];
num[rt]++;
}
}
void find(char *s)
{
int x,rt=0;
for(int i=0;s[i];i++)
{
x=s[i]-'a';
printf("%c", s[i]);
rt=t[rt][x];
if(num[rt]==1)
return ;
}
}
int main()
{
int c=0;
while(~scanf("%s",ss[c]))
insert(ss[c++]);
for(int i=0;i<c;i++)
{
printf("%s ",ss[i]);
find(ss[i]);
printf("\n");
}
return 0;
}

指针实现

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char ss[1001][21];
struct node
{
int vis;
struct node *next[26];
node()
{
vis=0;
for(int i=0;i<26;i++)
next[i]=NULL;
}
}*rt;
void insert(char *s)
{
node *p=rt;
for(int i=0;s[i];i++)
{
int x=s[i]-'a';
if(!p->next[x])
p->next[x]=new node;
p=p->next[x];
p->vis++;//访问次数增加
}
}
void find(char *s)
{
node *p=rt;
for(int i=0;s[i];i++)
{
printf("%c",s[i]);
int x=s[i]-'a';
p=p->next[x];
if(p->vis==1)//访问一次的就是独有的
return ;
}
}
int main()
{
rt=new node;
int c=0;
while(scanf("%s",ss[c])!=EOF)
insert(ss[c++]);
for(int i=0;i<c;i++)
{
printf("%s ",ss[i]);
find(ss[i]);
printf("\n");
}
return 0;
}

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. OpenJudge/Poj 2001 Shortest Prefixes

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

  7. POJ 2001 Shortest Prefixes (Trie)

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

  8. poj 2001 Shortest Prefixes trie入门

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

  9. poj2001 Shortest Prefixes(字典树)

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

随机推荐

  1. Java 进制间的转换

    package com.touch.onlinedu; public class Test { public static void main(String[] args) { // 1 : 0001 ...

  2. Android 5.0以上Material Design 沉浸式状态栏

    偶然在知乎上看到这个问题,Android 5.0 如何实现将布局的内容延伸到状态栏,之前也见过多个应用的这个功能,但是知乎上的答案却没有一个真正实现此功能的一类是把标题栏设置App主题颜色,一类是提取 ...

  3. 2018牛客暑期ACM多校训练营第一场(有坑未填)

    (重新组队后的第一场组队赛 也是和自己队友的一次磨合吧 这场比赛真的算是一个下马威吧……队友上手一看 啊这不是莫队嘛 然后开敲 敲完提交发现t了 在改完了若干个坑点后还是依然t(真是一个悲伤的故事)然 ...

  4. c++从文件路径获取目录

    场景 c++从文件路径获取目录 实现代码 初始化是不正确的,因为需要转义反斜杠: string filename = "C:\\MyDirectory\\MyFile.bat"; ...

  5. 【转】python之模块array

    [转]python之模块array >>> import array#定义了一种序列数据结构 >>> help(array) #创建数组,相当于初始化一个数组,如: ...

  6. Linux的notifier机制在TP中的应用【转】

    转自:https://blog.csdn.net/armfpga123/article/details/51771666 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog ...

  7. MySQL报错InnoDB: A long semaphore wait【转】

    mysql登录后无法执行命令如show processlist 查看MySQL错误日志 参考以下方法,执行 1.系统层面 [root@pisphkdcbsql01 ~]# cat /proc/sys/ ...

  8. Nand

    1.boolean logic 常用的boolean logic有AND OR NOT,其性质如下 事实上,可用AND和NOT来表示OR x or y = NOT(NOT(x) AND NOT(y)) ...

  9. Git学习笔记03-工作区和暂存区

    Git和其他版本控制工具不同的地方就是有暂存区的概念 工作区(Working Directory) 就是在电脑界面上能够看到的目录 版本库(Repository) 工作区下面有个一个.git文件夹,也 ...

  10. php7静态方法的链式调用

    2018-1-11 20:25:48 星期四 情景: 以前想要链式调用必须先 new 一个对象, 然后 $obj->aa()->bb()... 现在PHP7 (php7.0.13  php ...