POJ 2001 Shortest Prefixes
来源:互联网 发布:淘宝助理怎么导出图片 编辑:程序博客网 时间:2024/06/08 07:38
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
trie树~
好神奇的数据结构啊……
建树,记录每一个字母的出现次数,然后直接按每个单词顺序搜索,搜到只有它一个字母的时候就是最简写~
(又是一道没法在本机调试的题……提交过程简直酸爽……)
#include<cstdio>#include<cstring>#include<iostream>using namespace std;int ro[200001][26],tot[200001],cnt,kkz;char a[10001][21];void add(char s[]){int len=strlen(s+1),now=0;for(int i=1;i<=len;i++){int k=s[i]-'a';if(!ro[now][k]) ro[now][k]=++kkz;now=ro[now][k];tot[now]++;}}void findd(char s[]){int len=strlen(s+1),now=0;for(int i=1;i<=len;i++){if(tot[now]==1) return;int k=s[i]-'a';printf("%c",s[i]);now=ro[now][k];}}int main(){while(scanf("%s",a[++cnt]+1)==1) add(a[cnt]);for(int i=1;i<=cnt;i++){printf("%s",a[i]+1);printf(" ");findd(a[i]);printf("\n");}return 0;}
1 0
- 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 2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- poj 2001 Shortest Prefixes
- POJ-2001 Shortest Prefixes
- POJ 2001 Shortest Prefixes
- oracle function用法
- Android产品研发(八)-->App数据统计
- 29、SSH框架-Spring+Mybatis+SpringMVC+Maven+MySql搭建实例(4)
- 卷积神经网络在自然语言处理的应用
- 深度学习主机配置 gtx1080 + cuda + tensorflow + ubuntu16
- POJ 2001 Shortest Prefixes
- IIS配置勾选
- Javascri表单验证
- Android蓝牙开发浅谈 __ 耳机录音
- noip2014 寻找道路
- 网站开发之鼠标悬停简单特效实现(四)
- Humble Numbers 【数论】【DP】
- zookeeper集群安装和简单命令学习
- Android Linker 与 SO 加壳技术