poj2001Shortest Prefixes(trie)

Shortest Prefixes

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 18687   Accepted: 8087

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

题目大意:给出一堆字符串,给每一个字符串找最小的前缀,让所有的前缀都不重复。
字典树询问时,找到一个val为1的说明这个点只有他自己一个,所以到此结束。
注意数组的大小
 1 #include<cstdio>
 2 #include<cstring>
 3 #define MAXN 100100
 4 
 5 char s[1010][25];
 6 
 7 struct Trie
 8 {
 9     int ch[MAXN][26];
10     int val[MAXN];
11     int size;
12     Trie()
13     {
14         size = 1;
15         memset(ch,0,sizeof(ch));
16         memset(val,0,sizeof(val));
17     }
18     int id(char c)
19     {
20         return c-'a';
21     }
22     void Ins(char* s)
23     {
24         int u = 0, len = strlen(s);
25         for (int i=0; i<len; ++i)
26         {
27             int c = id(s[i]);
28             if (!ch[u][c]) ch[u][c] = size++;
29             u = ch[u][c];
30             val[u]++;
31         }
32     }
33     void Find(char* s)
34     {
35         int u = 0, len = strlen(s);
36         for (int i=0; i<len; ++i)
37         {
38             int c = id(s[i]);
39             u = ch[u][c];
40             printf("%c",s[i]);
41             if (val[u]==1) return ;
42         }
43     }
44 }t;
45 
46 int main()
47 {
48     int p = 0;
49     while (scanf("%s",s[++p])!=EOF)
50     {
51         t.Ins(s[p]);
52     }
53     for (int i=1; i<=p; ++i)
54     {
55         printf("%s ",s[i]);
56         t.Find(s[i]);
57         printf("\n");
58     }
59     return 0;
60 }

 

 
posted @ 2017-07-06 11:44  MJT12044  阅读(202)  评论(0编辑  收藏  举报