poj--2001--Shortest Prefixes(字典树)
来源:互联网 发布:职人咖啡 知乎 编辑:程序博客网 时间:2024/05/16 19:27
Shortest Prefixes
Time Limit: 1000MS Memory Limit: 30000KB 64bit IO Format: %I64d & %I64u
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
Source
Rocky Mountain 2004
#include<stdio.h>#include<string.h>int ch[10000+30][30];char str[1010][1000];int sz;int word[10010];void init(){sz=1;memset(ch[0],0,sizeof(ch[0]));memset(word,0,sizeof(word));}void insert(char *s){int l=strlen(s);int u=0;for(int i=0;i<l;i++){int c=s[i]-'a';if(!ch[u][c]){memset(ch[sz],0,sizeof(ch[sz]));//清空当前层 ch[u][c]=sz;//记录下一层的层数 sz++;}u=ch[u][c];word[u]++;//当前节点下的单词数加一 }}void find(char *s){int l=strlen(s);int u=0;for(int i=0;i<l;i++){int c=s[i]-'a';u=ch[u][c];//从第一层开始找,每次用u取下一层的层数 printf("%c",s[i]);if(word[u]==1)//word[u]==1表示当前节点只有一个单词 return ;}}int main(){int n=0;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(字典树)
- 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 (字典树!!)
- python numpy教程
- uva401 Palindromes 字符串,模拟
- cocos2d-js笔记
- 操作系统与网络实现 之六
- 谈面向对象之强依赖和弱依赖
- poj--2001--Shortest Prefixes(字典树)
- 各大团队的初始化CSS代码
- FormBorderStyle设为None,移动Winform窗口的两种方法
- Cordova插件开发(Android、iOS)
- 树的子结构
- linux下查看和添加PATH环境变量
- 接口返回值规则
- iOS nslog 使用 十六进制输出
- BestCoder Round #61 (div.2)(hdu5522,hdu5523,hdu5524,hdu5525(数论:费马小定理))