Shortest Prefixes
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16782   Accepted: 7286

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

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

题意:给出若干字符串,求把他们缩写所能形成的最小程度。

如:

abdef

abcdefg

ab

其缩写最小程度为

abd

abc

ab

可以使用字典树求出衍生边为1的各个单词,然后输出!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tree
{
char c;
struct tree *next[27];
int num;
};
void init(char *c,tree *T)
{
tree *p;
for(int i=0; i<(int)strlen(c); i++)
{
if(T->next[c[i]-'a']==NULL)
{
p=(tree*)malloc(sizeof(tree));
T->next[c[i]-'a']=p;
p->c=c[i];
T=T->next[c[i]-'a'];
memset(T->next,0,sizeof(T->next));
T->num=1;
}
else
{
T=T->next[c[i]-'a'];
T->num++;
}
}
}
void PR(char *c,tree *t)
{
t=t->next[c[0]-'a'];
for(int i=0; i<(int)strlen(c); i++)
{
if(t->num>1)
{
printf("%c",t->c);
t=t->next[c[i+1]-'a'];
}
else
{
printf("%c",t->c);
break;
}
}
}
int main()
{
char c[1005][35]={{0}};
int i;
tree *T=(tree*)malloc(sizeof(tree));
T->num=0;
memset(T->next,0,sizeof(T->next));
for(i=0; gets(c[i]); i++)
{
init(c[i],T);
}
for(int k=0; k<i; k++)
{
printf("%s ",c[k]);
PR(c[k],T);
printf("\n");
}
return 0;
}

POJ 2001:Shortest Prefixes的更多相关文章

  1. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  2. POJ 2001 Shortest Prefixes (Trie)

    题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...

  3. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  4. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  5. POJ 2001 Shortest Prefixes(字典树活用)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21651   Accepted: 927 ...

  6. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  7. poj 2001 Shortest Prefixes(字典树trie 动态分配内存)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15610   Accepted: 673 ...

  8. poj 2001 Shortest Prefixes trie入门

    Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...

  9. Shortest Prefixes(trie树唯一标识)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15948   Accepted: 688 ...

随机推荐

  1. C#资源文件和C#枚举如何结合使用?

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...

  2. TNS-01251: Cannot set trace/log directory under ADR

    试图改变监听日志的名称时,报出TNS-01251错误: $ lsnrctl LSNRCTL - Production on -JUN- :: Copyright (c) , , Oracle. All ...

  3. iptables使用

    iptables规则的查看.添加.删除和修改 1.查看 iptables -nvL --line-number (这个命令跟/etc/init.d/iptables status 输出差不多) -L ...

  4. CPU informition

    tar jxvf util-linux-ng-2.18.bz2cd util-linux-ng-2.18/./configure --enable-arch --enable-partx --enab ...

  5. HTML_css样式表 样式属性 格式布局

    CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控制精确, ...

  6. Codeforce Round #218 Div2

    A:没个元素的个数少的变成多的和就是了 B:居然被systemtest搓掉了- -分东西,我改的代码,还是shit一样的过的...别人的直接两个操作数相减就可以了! C:二分题- -,没想到比赛时因为 ...

  7. cmd进入某个目录

    love you my pig java枚举类 cmd进入某个目录 2011-04-06 15:49:38| 分类: 小知识 | 标签: |字号大中小 订阅     1.开始->运行->C ...

  8. oracle的冷备份

    oracle冷备份要备份三类文件:数据文件,控制文件,日志文件 查看所有数据文件 select name from v$datafile; 查看所有日志文件 select member from v$ ...

  9. 夺命雷公狗mongodb之----mongodb---1---的下载,安装,连接

    首先登录mongodb的官方网站即可进行下载: https://www.mongodb.com/download-center?jmp=nav#community 然后到wamp目录下创建一个mong ...

  10. linux下的网络配置

    临时生效的命令: 设置静态ip: ip addr add 192.168.1.2/24 dev eth0 设置网关: ip route add default via 192.168.1.1 启动某个 ...