POJ 2001 - Shortest Prefixes《字典树,求每个单词的最简化》

来源:互联网 发布:sql计算年龄语句 编辑:程序博客网 时间:2024/06/08 01:27

C - Shortest Prefixes
Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 2001

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


这个题主要是对最简化的理解-.-开始一直不理解。。。

只要那个单词有一个是他自己特有的元素。那他就是最简的了-.-

car是因为它就三个=.=再多也没有了

题解:

先把单词添加入树,并求出每个树出现的次数,

再查找最简树。。。。


代码:

#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;int n,wei;struct node{    char hh[22];    int ll;    int hao;char qianzui[22]; }ch[1100];struct trie{    struct trie *hai[26];    int op;}di[26000];struct trie *root;bool cmp(node xx,node yy){    int ll=min(xx.ll,yy.ll);    for (int i=0;i<=xx.ll;i++)    {        if (xx.hh[i]!=yy.hh[i])        return xx.hh[i]<yy.hh[i];    }}bool cm(node xx,node yy){return xx.hao<yy.hao;}void jia(int ii){char xx[22];strcpy(xx,ch[ii].hh);    struct trie *kk;    kk=root;int lp=-1;    int ll=strlen(xx);    for (int i=0;i<ll;i++)    {        if (!kk->hai[xx[i]-'a'])        {            kk->hai[xx[i]-'a']=&di[wei++];            memset(kk->hai[xx[i]-'a'],0,sizeof(trie));            kk->hai[xx[i]-'a']->op=0;        }        kk=kk->hai[xx[i]-'a'];        kk->op++;    }}void zhao(int ii){char xx[22];strcpy(xx,ch[ii].hh);    struct trie *kk;    kk=root;int lp=0;    int ll=strlen(xx);    for (int i=0;i<ll;i++)    {    if (!lp)    printf("%c",xx[i]);        kk=kk->hai[xx[i]-'a'];        if (kk->op==1)        lp=1;    }    printf("\n");}int main(){n=0;while (gets(ch[n].hh),ch[n].hh[0]){    ch[n].ll=strlen(ch[n].hh);    ch[n].hao=n;n++;}    sort(ch,ch+n,cmp);    wei=0;root=&di[wei++];    memset(root,0,sizeof(trie));    bool fafe=true;     for (int i=0;i<n;i++)    {        jia(i);    }    sort(ch,ch+n,cm);        for (int i=0;i<n;i++)    {    printf("%s ",ch[i].hh);    zhao(i);}    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 下压100上压160怎么办 怎么办去台湾新的驻签 学生去韩国旅游签证怎么办 猪的眼睛赛肿了怎么办 纹眉没有修复霜怎么办 衣服搞到走珠露香水洗不掉怎么办 涂牙膏把脸烧伤怎么办 月经血排不出来怎么办 猫眼角膜掉了一块怎么办 腰酸痛直不起腰怎么办 白血病移植后复发了怎么办 斐讯k2红灯常亮怎么办 洗衣机把烟洗了怎么办 吸烟吸的恶心想吐怎么办 显示双方信息的明细怎么办 电脑关不了机了怎么办 xp系统关不了机怎么办 灯的开关闭不了怎么办 灯的开关按不动怎么办 灯开关按不动了怎么办 灯的开关摁不动怎么办 微信群500人满了怎么办 作业帮搜不到题目怎么办呢 金融社保卡密码忘了怎么办 工作未完想从国企辞职怎么办 宫腔粘连术后流血多怎么办 宫腔粘连术后一直流血怎么办? cad图框放不下图怎么办 简历假的入职怎么办 脱贫攻坚怎么看怎么办怎么干 吃的下没力气怎么办 恢复留查公示后怎么办 微语简报删了怎么办 小学生未完成作业作为老师怎么办 抽调人员想回原单位怎么办 扫码支付没成功怎么办 成都电表卡丢了怎么办 社保未满15年怎么办 60岁社保没交满15年怎么办 柴油车dpf是亮了怎么办 贴双眼皮贴眼睛肿了怎么办