hdu1251在词典里统计前缀出现的个数

banana band bee absolute acm    ba b band abc

#include<iostream>
using namespace std;
//数据结构
struct Treenode{
int count;
Treenode *next[];
Treenode(){
count=;
for(int i=;i<;i++)
next[i]=NULL;
}
};
//插入建树
void insert(Treenode *&root,char *word){
if(root==NULL)
root=new Treenode();
Treenode *location=root;
int i=,branch=;
while(word[i])
{
branch=word[i]-'a';
if(location->next[branch])
location->next[branch]->count++;
else
location->next[branch]=new Treenode();
i++;
location=location->next[branch];
}
}
//查找
int search(Treenode *root,char *word)
{
if(root==NULL) return ;
Treenode *location=root;
int branch=,i=,ans;
while(word[i])
{
branch=word[i]-'a';
if(!location->next[branch])
return ;
i++;
location=location->next[branch];
ans=location->count;
}
return ans;
} int main()
{
char word[];
char ask[];
Treenode *root=NULL; while(gets(word))
{
if(word[]=='\0') break;
insert(root,word);
}
while(gets(ask))
{
cout<<search(root,ask)<<endl;
}
getchar();
return ;
}
上一篇:在Linux下查看环境变量


下一篇:Xcode打印如下错误:Unbalanced calls to begin/end appearance transitions 解决办法