POJ-2001--Shortest Prefixes (字典树)
来源:互联网 发布:android和linux的关系 编辑:程序博客网 时间:2024/05/01 12:32
Shortest Prefixes
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
字典树的题目。。。
题意就是找 能识别出每个单词所需的最少的长度(非公共前缀),并打印。如果这个最少的长度是它本身就打印本身。。
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>using namespace std;struct node{ int f; struct node *next[26];};struct node *build (){ struct node *t; t = (struct node *)malloc(sizeof (struct node)); for (int i = 0; i < 26; i++) { t -> next[i] = NULL; } t -> f = 0; return t;}void charu (struct node *t,char s[]){ int len = strlen (s); int ans; for (int i = 0; i < len ; i++) { ans = s[i] - 'a'; if (t ->next[ans] == NULL) { t -> next[ans] = build (); t = t->next[ans]; t -> f = 1; } else { t = t -> next[ans]; t -> f++; } }}void findd (struct node *t,char s[]){ int len = strlen (s); int ans; for (int i = 0; i < len ; i++) { printf ("%c",s[i]); ans = s[i] - 'a'; t = t -> next[ans]; if (t -> f == 1) break; }}void del (struct node *t){ for (int i =0 ; i < 26; i++) { if (t->next[i]!=NULL) { del (t->next[i]); } } free(t);}char a[100000][100];int main (){ int n = 0; struct node *tree ; tree = build (); while (~scanf ("%s",a[n])) { charu (tree,a[n]); n++; } for (int i = 0; i < n ; i++) { printf ("%s ",a[i]); findd (tree,a[i]); printf ("\n"); } // del(tree); 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 (字典树!!)
- POJ 1365 Prime Land
- $.ajax json数据传递方法
- 在ubuntu13.10版本上面架设开发mtk6572安卓环境
- 【U3D】Unity3D的基础认识
- MongoDB sharding
- POJ-2001--Shortest Prefixes (字典树)
- 网络访问频繁的项目---ThreadManager(线程池管理类)
- CSU-ACM暑假集训基础组七夕专场 D - Problem D
- Free Candies - UVa 10118 dp
- 从零开始学习Zstack之2
- synchronized详解
- 为RT-Thread实现一个傻瓜式的工程向导工具(中期报告)
- CSU-ACM暑假集训基础组七夕专场 F - Problem F
- 从零开始学习Zstack之3