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
題意:
查找單詞的最短前綴
代碼如下:
#include<stdio.h>
#include<string.h>
#define?maxn?10000
char?dic[maxn][21];
#define?M?10000000
struct?Dict
{
????int?times,?isWord;
????Dict?*?next[26];
};
Dict?F[M];//供newNode用的,比malloc快。
int?th?=?0;//th表示F被調用過的空間
Dict?*?newNode()//
{
????Dict?*?p?=?&F[th++];
????p->isWord?=?0,?p->times?=?0;//初始化P
????for?(int?i?=?0;?i?<?26;?i++)
????{
????????p->next[i]?=?NULL;
????}
????return?p;
}
void?insert(Dict?*?r,?char?*?s)
{
????if?(s[0]?==?0)
????{
????????r->isWord++;//表示單詞數(shù)量
????????return;
????}
????if?(!r->next[s[0]-'a'])//如果s[0]不是結束符,且該單詞前綴不存在,生成當前字母
????{
????????r->next[s[0]-'a']?=?newNode();
????}
????r->next[s[0]-'a']->times++;//該單詞當前前綴出現(xiàn)次數(shù)
????insert(r->next[s[0]-'a'],?s?+?1);
}
//符合的前綴條件:
//前綴是唯一的,或者以該單詞本身做自己的前綴。
int?search(Dict?*?r,?char?*?s,?int?len)
{
????if?(s[len]?==?0)
????{
????????if?(r->isWord)//表示該前綴是個單詞。
????????{
????????????return?len;
????????}
????????return?-1;
????}
????if?(r->times?==?1)//表示該前綴唯一
????{
????????return?len;
????}
????if?(!r->next[s[len]-'a'])
????{
????????return?-1;
????}
????return?search(r->next[s[len]-'a'],?s,?len?+?1);
}
int?main()
{
????int?i,?j,?k,?t,?len;
????char?a[21];
????Dict?*?root?=?newNode();
????for?(i?=?0;?scanf("%s",?dic[i])?!=?EOF;?i++)
????{
????????insert(root,?dic[i]);
????}
????for?(j?=?0;?j?<?i;?j++)
????{
????????printf("%s?",?dic[j]);
????????len?=?search(root,?dic[j],?0);
????????dic[j][len]?=?0;
????????printf("%s\n",?dic[j]);
????}
????return?0;
}