Language:
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 20503 Accepted: 8863
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

字典树..
code:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;int tot;
char a[1010][30];
int sz,tr[500000][30],nm[500000];void insert(char ch[]) {int len=strlen(ch+1),now=0,k;for(int i=1;i<=len;i++) {k=ch[i]-'a';if(!tr[now][k]) tr[now][k]=++sz;now=tr[now][k];nm[now]++;}
}void ask(char ch[]) {int len=strlen(ch+1),now=0,k;for(int i=1;i<=len;i++) {if(nm[now]==1) break;k=ch[i]-'a';printf("%c",ch[i]);now=tr[now][k];}
}int main() {while(~scanf("%s",a[++tot]+1)) insert(a[tot]);for(int i=1;i<=tot;i++) {printf("%s ",a[i]+1);ask(a[i]);printf("\n");}return 0;
}

转载于:https://www.cnblogs.com/Menteur-Hxy/p/9247972.html

[poj 2001] Shortest Prefixes (字典树)相关推荐

  1. OpenJudge/Poj 2001 Shortest Prefixes

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

  2. poj 2001 Shortest Prefixes(特里)

    主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...

  3. Poj 2001 Shortest Prefix(字典树模板)

    字典树入门题...... 只需在对应结点记录num值,找到num为1时的前缀,是唯一前缀(只是该单词的前缀),或者一直找找到该单词结尾. #include <iostream> #incl ...

  4. POJ - 2513 Colored Sticks(字典树+并查集+欧拉回路)

    题目链接:点击查看 题目大意:给出n个木棍,问若两两相连,最终能否构成一根长直木棍,相连的规则是两个木棍的相接端点的颜色需要保持相同 题目分析:关于这个题目,我们可以将每个木棍视为一条边,每个木棍的两 ...

  5. POJ - 3630 Phone List(字典树)

    题目链接:点击查看 题目大意:给出n个电话号码,问是否都能拨通,拨通的定义是不会存在一个电话号码是另一个电话号码的前缀 题目分析:因为涉及到了前缀,所以我们可以直接套字典树的板子上去,按照题意插入一遍 ...

  6. POJ 1451 T9 (字典树好题)

    背景:为了方便九宫格手机用户发短信,希望在用户按键时,根据提供的字典(给出字符串和频数),给出各个阶段最有可能要打的单词. 题意: 首先给出的是字典,每个单词有一个出现频率.然后给出的是询问,每个询问 ...

  7. poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)

    题目:http://poj.org/problem?id=2513 参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445 htt ...

  8. 【POJ - 2001 】Shortest Prefixes (字典树,查询重复前缀区间)

    题干: A prefix of a string is a substring starting at the beginning of the given string. The prefixes ...

  9. Shortest Prefixes(字典树)

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

最新文章

  1. 倍增时间 倍增...?
  2. 66319d电源使用说明书_矿用防爆交换机如何正确安装调试和使用?
  3. C# 打开文件/跳转链接
  4. c#利用zlib.net对文件进行deflate流压缩(和java程序压缩生成一样)
  5. 图像坐标球面投影_比较常用的坐标几种投影
  6. Django 缓存、序列化、信号
  7. 通过真实项目截图讲解MDT 2010部署windows 7的具体过程(3)
  8. vue3 three ts 全景图
  9. BME/BMP280环境传感器、MLX90614红外测温传感器、HX711称重模块 | Mixly纯干货课程
  10. Barrett And Montgomery of Polynomials
  11. 如何从outlook ost恢复数据(ost导入outlook)
  12. 最常用的五个网页JS特效代码:收藏本站、设为首页、刷新本页
  13. 安全生产管理云执法平台方案
  14. 【C语言基础-」TurboC环境】-」bioskey函数详细分析
  15. 好听的歌曲单片机c语言程序,单片机C语言程序设计:播放生日快乐歌
  16. 标普全球普氏推出区块链网络追踪阿联酋的石油数据
  17. 【深度学习】实验1答案:Softmax实现手写数字识别
  18. 错觉图片生成实验 - 平行线(黑林错觉)
  19. 基于74ls194的m序列发生器(硬件)
  20. A Modified Particle Swarm Optimizer

热门文章

  1. Oracle常用知识
  2. Synchronized 天天用,实现原理你懂吗?
  3. 神仙打架:PG 和 MySQL 到底哪个更好用?
  4. 职场程序员如何高效自学
  5. Nginx 配置文件 nginx.conf 详解
  6. 程序员,你是选择25k的996还是18k的八小时工作日
  7. Eclipse深度患者设置VSCode快捷键
  8. 一套mysql_mysql 命令一套
  9. php host头攻击,Apache/Nginx中Host头攻击的一些差异
  10. java reactor nio_java reactor与NIO