模板大致如下:具体用法可参见上篇Poj2503第三种解法
struct TriedNode
{
int No;
TriedNode *branch[BRANCH_MAX];
};
TriedNode root;
TriedNode node[10010];
void Insert(char *word, char *re)
{
if (NULL == word)
{
return;
}
TriedNode *location = &root;
while(*word)
{
if (NULL == location->branch[*word - 'a'])
{
location->branch[*word - 'a'] = &node[p++];
}
location = location->branch[*word - 'a'];
++word;
}
strcpy(location->re, re);
}
void Search(char *res, char *word)
{
if (NULL == word)
{
return;
}
TriedNode *location = &root;
while(*word)
{
if (location->branch[*word - 'a'])
{
location = location->branch[*word - 'a'];
}
else
{
strcpy(res, "eh");
return;
}
++word;
}
strcpy(res, location->re);
}