Shortest Prefixes POJ - 2001(字典树)
2017-11-07 08:55
405 查看
Shortest Prefixes
POJ - 2001A 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的时候,说明这个前缀只有
这个字符串有,这个前缀即为所求前缀
code:
#include <iostream> #include <cstdio> #include <cstring> #include <stdlib.h> using namespace std; char s[1009][35];//存放字符串 struct TNode{ int time; struct TNode* Next[26];//字典树结构体 }tre; typedef struct TNode* Tree; void Insert(char a[]){//字典树插入 int i,j; Tree p = &tre; Tree q; for(i = 0; a[i]; i++){ if(p->Next[a[i]-'a']==NULL){ q = (Tree)malloc(sizeof(struct TNode)); q->time = 0; for(j = 0; j < 26; j++)q->Next[j] = NULL; p->Next[a[i]-'a'] = q; } p = p->Next[a[i]-'a']; p->time++; } } void Find(char a[]){ int i,j; Tree p = &tre; for(i = 0; a[i]; i++){ printf("%c",a[i]);//每次输出一个字符 p = p->Next[a[i]-'a']; if(p->time==1){//判断这个字符,以这个字符为末尾的前缀出现次数是否为1,是1就终止,当前输出的即所求前缀 return; } } } int main(){ int i; int cnt = 0; for(i = 0; i < 26; i++)tre.Next[i] = NULL;//初始化头节点 while(~scanf("%s",s[cnt++])); for(i = 0; i < cnt; i++){ Insert(s[i]);//输入 } for(i = 0; i < cnt; i++){ printf("%s ",s[i]); Find(s[i]);//查找 puts(""); } return 0; }
相关文章推荐
- 字典树---2001 POJ Shortest Prefixes(找最短前缀)
- 字典树---2001 POJ Shortest Prefixes(找最短前缀)
- Shortest Prefixes POJ - 2001 (字典树模板题~)
- poj 2001 Shortest Prefixes ——字典树入门
- [字典树] poj 2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
- POJ 2001 Shortest Prefixes 字典树
- POJ 2001 Shortest Prefixes 字典树Trie
- poj 2001 Shortest Prefixes 字典树的输出问题的解释~~ 2012-4-14
- 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 字典树(模版题)
- POJ2001 Shortest Prefixes 动态字典树实现
- [复习][poj2001]字典树(trie树)Shortest Prefixes
- POJ 2001 Shortest Prefixes【字典树】
- POJ 2001 Shortest Prefixes【第一棵字典树】