前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 2001 Shortest Prefixes

POJ 2001 Shortest Prefixes

作者头像
attack
发布2018-04-12 14:29:01
5680
发布2018-04-12 14:29:01
举报

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

代码语言:javascript
复制
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate

Sample Output

代码语言:javascript
复制
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona

Source

Rocky Mountain 2004

裸的trie。

设置一个访问数组。

每次枚举就好

代码语言:javascript
复制
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<queue>
 6 #include<algorithm>
 7 #include<cstdlib>
 8 using namespace std;
 9 const int MAXN=100001;
10 void read(int &n)
11 {
12     char c='+';int x=0,flag=1;
13     while(c<'0'||c>'9')
14     {c=getchar();if(c=='-')flag=-1;}
15     while(c>='0'&&c<='9')
16     {x=x*10+c-48;c=getchar();}
17     n=(x*flag);
18 }
19 struct TRIE
20 {
21     int ch[MAXN][51];
22     int sz;
23     int val[MAXN];
24     TRIE()
25     {memset(ch[0],0,sizeof(ch[0]));
26     memset(val,0,sizeof(val));
27     sz=1;}
28     int idx(char c)
29     {
30         return c-'a';
31     }    
32     void Insert(char *s)
33     {
34         int l=strlen(s);int now=0;
35         for(int i=0;i<l;i++)
36         {
37             char c=idx(s[i]);
38             if(!ch[now][c])
39             {
40                 memset(ch[sz],0,sizeof(ch[sz]));
41                 ch[now][c]=sz++;
42             }
43             now=ch[now][c];
44             val[now]++;
45             //printf("%d ",val[now]);
46         }
47     }
48     void query(char *s)
49     {
50         int l=strlen(s);
51         int now=0;
52         for(int i=0;i<l;i++)
53         {
54             int c=idx(s[i]);
55             now=ch[now][c];
56             printf("%c",s[i]);    
57             if(val[now]==1)
58             return ;
59         }
60     }
61 }trie;
62 char s[MAXN][201];
63 int n=1;
64 
65 int main()
66 {
67     
68     while(scanf("%s",s[n])==1)
69     {
70         n++;
71         trie.Insert(s[n-1]);
72     }
73     for(int i=1;i<n;i++)
74     {
75         printf("%s ",s[i]);
76         trie.query(s[i]);
77         printf("\n");
78     }
79     return 0;
80 } 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-07-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档