hdu1251在词典里统计前缀出现的个数
2024-10-19 14:52:06
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 ;
}
最新文章
- 通过配置http拦截器,来进行ajax请求验证用户登录的页面跳转
- Atitit。木马病毒原理机密与概论以及防御
- Linux初学 - 解决chkconfig Segmentation fault(core dumped)
- WordPress模板常用函数
- python自学笔记一
- 移植FastBlur模糊算法至SDL
- linux c遍历文件夹 和文件查找的方法
- 转:三十一、Java图形化界面设计——布局管理器之GridLayout(网格布局)
- Linux系统编程(14)——shell常用命令
- ios像素点颜色取样
- freemarker 遍历 hashmap 到select option
- Spring Cloud 入门教程 - Eureka服务注册与发现
- 在WINDOWS中安装使用GSL(MinGW64+Sublime Text3 &; Visual Studio)
- JQuery——选择器
- Python 3+selenium+unittest+HTMLTestRunner生成测试报告
- CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-8CDH5安装和集群配置
- Java中有关Null的9件事(转)
- Linux查看操作系统版本命令
- sql server 语法 MSDN
- centos7 静默安装oracle