AmazingCounters.com

POJ2001 (Shortest Prefixes)

Shortest Prefixes
Time Limit: 1000MS   Memory Limit: 30000K
     

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

Source

 
第一道trie。。题目求的是每个字符串的的前缀且这个前缀不是其他字符串的前缀。
 1 #include<set>
 2 #include<map>
 3 #include<queue>
 4 #include<cstdio>
 5 #include<cstdlib>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 using namespace std;
10 const int N = 30;
11 const int MAXN = 1010;
12 #define For(i,n) for(int i=1;i<=n;i++)
13 #define Rep(i,l,r) for(int i=l;i<=r;i++)
14 
15 char word[MAXN][N],ans[N];
16 int n;
17 
18 struct trie{
19     int sz,ch[MAXN*N][N],v[MAXN*N];
20     trie(){sz=1;}
21     void insert(char st[]){
22         int len=strlen(st);int now=1;v[now]++;
23         Rep(i,0,len-1){
24             int id=st[i]-'a';
25             if(!ch[now][id]) ch[now][id]=++sz;
26             v[now=ch[now][id]]++;
27         }
28     }
29     void prefix(char st[],char *ans){
30         int now=1,len=strlen(st);
31         Rep(i,0,len-1){
32             int id=st[i]-'a';
33             if(v[now]==1){ans[i]='\0';return;}
34             ans[i]=st[i];
35             now=ch[now][id];
36         }
37         ans[len]='\0';
38     }
39 }trie;
40 
41 int main(){
42     #ifndef ONLINE_JUDGE
43         freopen("prefix.in","r",stdin);
44     #endif
45     for(n=1;scanf("%s",word[n])!=EOF;n++) trie.insert(word[n]);
46     For(i,n) {
47         trie.prefix(word[i],ans);
48         printf("%s %s\n",word[i],ans);
49     }
50     return 0;
51 }

 

posted @ 2014-09-12 10:46  ZJDx1998  阅读(205)  评论(0编辑  收藏  举报