poj 2001 Shortest Prefixes 【字典树】【找每一个字符串在字符串集里面的 最短且可唯一标识 的前缀】
来源:互联网 发布:mac升级系统卡住开机 编辑:程序博客网 时间:2024/05/21 12:40
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 14747 Accepted: 6364
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
第一道字典树:0ms
#include <cstdio>#include <cstring>#define MAX 10000+10using namespace std;char str[1010][30];int ch[MAX][30];int word[MAX];//记录当前节点下有几个单词 int n = 0;//单词个数 int sz;//节点数 void init(){ sz = 1;//只有一个根节点 memset(ch[0], 0 ,sizeof(ch[0])); memset(word, 0, sizeof(word));}int idx(char x)//字母编号 { return x-'a';}void insert(char *s)//插入 { int i, j, l = strlen(s); int u = 0; for(i = 0; i < l; i++) { int c = idx(s[i]); if(!ch[u][c])//节点不存在 { memset(ch[sz], 0, sizeof(sz));//初始化新节点 ch[u][c] = sz; sz++; //节点数自增一 } u = ch[u][c]; word[u]++;//单词数加一 }}void find(char *s)//查询 { int i, j, l = strlen(s); int u = 0;//从根节点开始 for(i = 0; i < l; i++) { int c = idx(s[i]); u = ch[u][c]; printf("%c", s[i]); if(word[u] == 1)//当前前缀足以找出单词 return ; }}int main(){ init(); while(scanf("%s", str[n]) != EOF) { insert(str[n]);//插入 n++; } for(int i = 0; i < n; i++) { printf("%s ", str[i]); find(str[i]); printf("\n"); } return 0;}
0 0
- poj 2001 Shortest Prefixes 【字典树】【找每一个字符串在字符串集里面的 最短且可唯一标识 的前缀】
- 找出能唯一标示一个字符串的最短前缀 Trie POJ 2001 Shortest Prefixes
- Shortest Prefixes 【字典树 求给定字符串的 最短确定前缀】
- POJ Shortest Prefixes -- 最短前缀问题 -- 字典树
- poj 2001 Shortest Prefixes 字典树找前缀
- POJ 2001 Shortest Prefixes 短的前缀 代码整理
- POJ 2001 Shortest Prefixes 字典树经典题,求最短唯一前缀)
- 字典树---2001 POJ 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 字典树Trie
- POJ 2001 Shortest Prefixes(字典树Trie)
- poj 2001 Shortest Prefixes(字典树)
- [字典树] poj 2001 Shortest Prefixes
- Android在fragment中调用getacvitity()返回null的解决方法
- jQuery的三种$()
- 漂亮UI的全能音乐播放器
- 根据用户权限显示共享文件夹-windows 2003, Access-based Enumeration
- JAVA自定义注解(Target,Retention,Documented,Inherit)
- poj 2001 Shortest Prefixes 【字典树】【找每一个字符串在字符串集里面的 最短且可唯一标识 的前缀】
- 关于破解WIFI密码的最简单方法
- jxl 各数据类型格式配置
- 唐山自闭症治疗好yiyuan
- ExtJs TreePanel 动态加载
- 图像的预处理操作
- Android UI开发第十四篇——可以移动的悬浮框
- 唐山自闭症治疗好的yiyuan
- BP神经网络的详细解析:思路清晰强烈推荐