POJ【2001】Shortest Prefixes----字典树
来源:互联网 发布:域名在哪注册 编辑:程序博客网 时间:2024/05/01 21:00
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".
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
carbohydratecartcarburetorcaramelcariboucarboniccartilagecarboncarriagecartoncarcarbonate
Sample Output
carbohydrate carbohcart cartcarburetor carbucaramel caracaribou caricarbonic carbonicartilage carticarbon carboncarriage carrcarton cartocar carcarbonate carbona
题大意:找每个字符串唯一,且最小的前缀
code:#include<stdio.h>#include<string.h>#include<stdlib.h>struct node{ int cs; int jd; struct node*next[26];};struct node*shenqing(){ struct node*p; p=(struct node*)malloc(sizeof(struct node)); p->cs=0; p->jd=0; for(int i=0;i<26;i++) { p->next[i]=NULL; } return p;}void jianshu(struct node*p,char s[]){ int len=strlen(s); struct node*tree=p; for(int i=0;i<len;i++) { int ans=s[i]-'a'; if(tree->next[ans]==NULL) tree->next[ans]=shenqing(); tree=tree->next[ans]; tree->cs++; if(i==len-1) tree->jd++; }}void sc(struct node*p,char s[]){ int len=strlen(s); struct node*tree=p; if(tree!=NULL) { for(int i=0;i<len;i++) { int ans=s[i]-97; if(tree->next[ans]!=NULL) { printf("%c",ans+'a'); tree=tree->next[ans]; if(i==len-1) { tree->jd--; return; } if(tree->cs<=1) return; } } }}int main(){ struct node *tree; tree=shenqing(); char s[1009][25]; int t=0,i,j,k; while(scanf("%s",s[t])!=EOF) { jianshu(tree,s[t]); t++; } for(i=0;i<t;i++) { printf("%s ",s[i]); sc(tree,s[i]); printf("\n"); } return 0;}
0 0
- POJ 2001:Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- poj 2001 -- Shortest Prefixes ( 字典树 )
- POJ 2001 Shortest Prefixes ( 字典树 )
- POJ 2001 Shortest Prefixes 字典树Trie
- POJ 2001 Shortest Prefixes(字典树Trie)
- poj 2001 Shortest Prefixes(字典树)
- [字典树] poj 2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes 字典树
- POJ 2001-Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes(字典树)
- POJ 2001 Shortest Prefixes(字典树)
- POJ【2001】Shortest Prefixes----字典树
- POJ-2001--Shortest Prefixes (字典树)
- poj 2001 Shortest Prefixes 字典树
- poj 2001 Shortest Prefixes(字典树)
- poj 2001 Shortest Prefixes(字典树)
- POJ - 2001 - Shortest Prefixes (字典树!!)
- 杭电2045不容易系列之(3)—— LELE的RPG难题(递推涂颜色)----20140731
- [LeetCode] Binary Tree Postorder Traversal [42]
- OpenCV学习笔记【6】:从摄像机读入数据
- JavaScript提高:003:easy UI实现tab页面自适应问题
- 使用数组初始化vector
- POJ【2001】Shortest Prefixes----字典树
- 蔫奇撑羞铆继躺洲量贷新匹癸
- 局域网共享问题全方位解决
- POJ 1276 Cash Machine (DP)
- 简单2D飞行游戏练习
- 微软、IBM、亚马逊在云计算领域展开激烈竞争
- ffgetetgertee
- Android Fragment 基本介绍
- 整理音乐(插入排序)