【poj2001】Shortest Prefixes 字典树
来源:互联网 发布:礼券自助提货系统源码 编辑:程序博客网 时间:2024/05/16 12:07
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”.
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
字典树,不过题目给的数据范围给错了,单词数可能大于1000
记录次数即可
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int SZ = 10010; struct trie{ int id,t;}tree[SZ][30];int n = 0;char s[SZ][1010];void insert(char s[]){ int p = 0; for(int i = 0;i < strlen(s);i ++) { if(!tree[p][s[i] - 'a'].id) tree[p][s[i] - 'a'].id = ++ n; tree[p][s[i] - 'a'].t ++; p = tree[p][s[i] - 'a'].id; }// puts("");}char ans[SZ];void ask_ans(char s[]){ int p = 0; memset(ans,0,sizeof(ans)); for(int i = 0;i < strlen(s);i ++) { ans[i] = s[i]; if(tree[p][s[i] - 'a'].t == 1) return ; p = tree[p][s[i] - 'a'].id; }}int main(){ int n = 1; while(cin>>s[n]) insert(s[n]),n ++; for(int i = 1;i <= n - 1;i ++) { ask_ans(s[i]); printf("%s %s\n",s[i],ans); } return 0;}
- POJ2001 - Shortest Prefixes - 字典树
- POJ2001 Shortest Prefixes【字典树】
- 【poj2001】Shortest Prefixes 字典树
- POJ2001 Shortest Prefixes(字典树)
- POJ2001 Shortest Prefixes(字典树)
- kyeremal-poj2001-Shortest Prefixes-字典树trie
- POJ2001 Shortest Prefixes 动态字典树实现
- [复习][poj2001]字典树(trie树)Shortest Prefixes
- 字典树trie树 模板题 POJ2001 Shortest Prefixes
- poj2001 Shortest Prefixes (trie树)
- poj2001 Shortest Prefixes trie树
- 【Trie树】POJ2001 Shortest Prefixes
- POJ2001 Shortest Prefixes trie树模板
- Shortest Prefixes(poj2001,tire树)
- POJ2001 Shortest Prefixes
- poj2001 Shortest Prefixes
- 【poj2001】Shortest Prefixes
- POJ2001--Shortest Prefixes
- 使用UncaughtExceptionHandler重启线程
- 再次复习java正则表达式
- Baidu地图的相关开发经验
- Python中的下划线(译文)
- Java停止线程
- 【poj2001】Shortest Prefixes 字典树
- VMware虚拟机上网络连接(network type)的三种模式--bridged、host-only、NAT
- YTU 1009: University
- 记一次Ubuntu突然无法进入系统的经历
- cookie 和session 的区别详解
- web开发中文字符乱码解决集合文GOOD
- fmdb使用
- Activity
- Hibernate 注解大全