标签:
struct node{
int Count;
node *next[26];
node(){ //初始化数据
memset(next,NULL,sizeof(next));
Count=0;
}
};
node *p,*root=new node();
void Insert(char *s)//插入新单词
{
int i,k;
for(p=root,i=0;s[i];++i)
{
k=s[i]-'a';
if(p->next[k]==NULL) p->next[k]=new node();
p=p->next[k];
p->Count++; //记录此字母出现的次数
}
}
int Search(char *s)
{
int i,k;
for(p=root,i=0;s[i];++i)
{
k=s[i]-'a';
if(p->next[k]==NULL) break; //一旦查找不到,立即跳出
p=p->next[k];
}
if(s[i]) return 0;//s[i]!=0表示中间
return p->Count;
}标签:
原文地址:http://blog.csdn.net/u013445530/article/details/42422409