Shortest Prefixes
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
这题是字典树的裸题,给出一堆字符串,问每一个字符串的独一无二的缩写,其实就是求每一个串的唯一最短前缀,我们把这些字符串建立一颗字典树,trie[u]保存u的儿子,既该字符u的下一个字符,由cnt[u]保存的是当前字符的出现次数,如果只出现一次,或者没有儿子则其就是所求前缀
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxNode=10005*25;
const int sigma_size=26;
int trie[maxNode][sigma_size];
int cnt[maxNode];
int val[maxNode];
int sz;
char s[25];
char out[1005][30];
int k=0;
void init()
{
sz=1;
memset(cnt,0,sizeof(cnt));
memset(trie[0],0,sizeof(trie));
}
int getid(char ch)
{
return ch-'a';
}
void build(char s[])
{
int u=0;
int len=strlen(s);
for(int i=0;i<len;i++)
{
char ch = getid(s[i]);
if(!trie[u][ch])
{
memset(trie[sz],0,sizeof(trie[sz]));
trie[u][ch]=sz++;
}
u=trie[u][ch];
cnt[u]++;
}
}
void find(char t[])
{
int u=0;
int len =strlen(t);
for(int i=0;i<len;i++)
{
char ch=getid(t[i]);
if(!trie[u][ch]||u!=0&&cnt[u]<=1)//重要的判断条件
{
break;
}
printf("%c",t[i]);
u=trie[u][ch];
}
}
int main()
{
init();
while(scanf("%s",s)!=EOF)
{
build(s);
strcpy(out[k],s);
k++;
}
for(int i=0;i<k;i++)
{
printf("%s ",out[i]);
find(out[i]);
printf("\n");
}
return 0;
}