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
题意
输入若干单词,求这些单词中每个单词的最短前缀----这个前缀就只这一个单词有。
如果没有这样的最短前缀,就输出这一整个单词。
题解
把这些单词建成一个字典树,在建树的过程中,每经过一个节点,就用一个辅助数组把这个节点加一次,vis[root],存的是从根节点开始到下标为root的节点这一路的字母即前缀,有多少个单词有这样的前缀。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#define INF 0x3f3f3f3f
#define fori(a,b) for(int i=a;i<b;i++)
#define forj(a,b) for(int j=a;j<b;j++)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
//const double PI = acos(-1);
const int MAX=1e5;
char s[1111][50];
int trie[MAX][30];  //trie[i][j]=k,下标为i,字母为j的节点的j的后边字母的下标
int vis[MAX];
int cnt=1;
void build_trie(char s[])    //建树
{int root = 0;fori(0,s[i]){int next = s[i]-'a';if(!trie[root][next])trie[root][next]=cnt++;root=trie[root][next];vis[root]++;}
}string query(char s[])    //查询
{string ans;int root=0;fori(0,s[i]){if(vis[root]==1)return ans;int next=s[i]-'a';ans+=s[i];root=trie[root][next];}return ans;
}
int main()
{mem(trie,0);mem(vis,0);int n=0;while(scanf("%s",s[n])!=EOF){build_trie(s[n]);n++;}fori(0,n){cout << s[i] << " "<< query(s[i]) << endl;}
}

Shortest Prefixes(字典树)相关推荐

  1. [poj 2001] Shortest Prefixes (字典树)

    Language: Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20503 Accepte ...

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

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

  3. BZOJ 3483 SGU505 Prefixes and suffixes(字典树+可持久化线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3483 [题目大意] 给出一些串,同时给出m对前缀后缀,询问有多少串满足给出的前缀后缀模 ...

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

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

  5. 字符串处理——字典树

    [概述] 字典树,又称为单词查找树,Tire 树,是一种树形结构,它是哈希树的变种. 字典树与字典很相似,当要查一个单词是不是在字典树中,首先看单词的第一个字母是不是在字典的第一层,如果不在,说明字典 ...

  6. Bailian2797 最短前缀【字典树】

    2797:最短前缀 总时间限制: 1000ms 内存限制: 65536kB 描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的字串是: &quo ...

  7. Trie(前缀树/字典树)及其应用

    from:https://www.cnblogs.com/justinh/p/7716421.html Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,P ...

  8. Trie树(字典树)详细知识点及其应用

    Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...

  9. 【CODE】Unique Paths Word Search (DFS dp 字典树)

    目录 62. Unique Paths 63. Unique Paths II 980. Unique Paths III 79. Word Search 212. Word Search II 字典 ...

最新文章

  1. 大佬原创 | 深度学习60讲453页pdf下载
  2. python批量下载文件-Python实现批量下载文件
  3. Camelot:从pdf中提取表格数据
  4. thinkpad t400无线网卡故障恢复技巧
  5. UML-2-迭代、进化和敏捷
  6. 二、创作网站 (三) Creating custom content types
  7. LINUX内核经典面试题
  8. eclipse下如何打开被关闭的项目_如何在Windows 10中打开和关闭平板电脑模式
  9. 专利翻译常用计算机词汇,基于NP树的英文专利文献术语自动翻译技术研究-计算机应用技术专业论文.docx...
  10. auto.js适合安卓小米6,朋友圈触控点赞
  11. 移动路由器刷linux,Linux小课堂开课了(7)-三线IP刷路由-移动-联通-电信
  12. 上面两点下面一个三角形_【人人都能欣赏的数学证明】为什么三角形的三个内角相加是180度?...
  13. iPhoneXR苹果手机中文电路原理图纸
  14. protel99SE - 多张原理图生成一张总网表的方法
  15. php动态万年历模块,php如何实现万年历的开发(每日一课真是非常有效率)
  16. c语言 宏常量 pi,宏和常量
  17. 2018计算机专硕学硕,2018年专硕和学硕的初试和复试的区别?
  18. NPOI操作Excel 让单元格的内容换行
  19. java计算机毕业设计奇妙店铺电子商务网站源码+系统+数据库+lw文档+mybatis+运行部署
  20. w10服务器优化,Win10专业版怎么优化最流畅?Win10最详细优化设置告别卡顿!

热门文章

  1. 51单片机矩阵键盘C程序
  2. R语言 plot swimmer_R语言基础知识学习(四):R中的画图函数--plot()函数
  3. FMEA软件——聊聊FMEA那些事
  4. nice-learning
  5. hp380G10服务器安装centos6.9和显卡驱动
  6. think-cell高阶教程(1)——单一窗口管理所有数据链接的自动更新
  7. 关于Android AlarmManager的一些知识(内容转载)
  8. 厦门大学 数据库实验室(林子雨)
  9. js 金钱格式化(money)
  10. 中兴F660端口映射