hdu1251
来源:互联网 发布:皇朝娱乐关注软件 编辑:程序博客网 时间:2024/06/05 18:31
题意:统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
字典树 今天晚上刚学的。。算法无涯啊。好多东西都不知道。
/*
Sample Input
bananabandbeeabsoluteacmbabbandabc
Sample Output
2310*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct dirtree
{
struct dirtree *child[26];
int n;
};
struct dirtree *root;
void Insert(char*source)
{
int i,j,len;
len=strlen(source);
if(len==0)return ;
struct dirtree *current,*newnode;
current=root;
for(i=0;i<len;i++)
{
if(current->child[source[i]-'a']!=0)
{
current=current->child[source[i]-'a'];
current->n=current->n+1;
}
else
{
newnode=(struct dirtree*)malloc(sizeof(struct dirtree));
for(j=0;j<26;j++)
{
newnode->child[j]=0;
}
current->child[source[i]-'a']=newnode;
current=newnode;
current->n=1;
}
}
}
int Find(char *source)
{
struct dirtree *current;
int len=strlen(source);
if(len==0)return 0;
current=root;
for(int i=0;i<len;i++)
{
if(current->child[source[i]-'a']!=0)
current=current->child[source[i]-'a'];
else
return 0;
}
return current->n;
}
int main()
{
char temp[11];
int i,j;
root=(struct dirtree*)malloc(sizeof(struct dirtree));
for(i=0;i<26;i++)
root->child[i]=0;
root->n=2;
while(gets(temp),strcmp(temp,"")!=0)
Insert(temp);
while(scanf("%s",temp)!=EOF){
i=Find(temp);
printf("%d/n",i);
}
return 0;
}
- hdu1251
- HDU1251
- hdu1251
- hdu1251
- hdu1251
- hdu1251
- hdu1251
- hdu1251
- hdu1251
- HDU1251
- hdu1251
- hdu1251
- HDU1251.cpp
- hdu1251 字符统计
- 统计难题 hdu1251
- HDU1251字典树
- hdu1251 字典树
- HDU1251统计难题
- POJ1251题的代码
- hdu1711 Number Sequence
- pku3487(稳定婚姻问题)
- 对你工作的几条忠告
- windows 下qt 连接mysql
- hdu1251
- mongodb小结(转)
- 获取汉字拼音(繁体转简体)
- 数据结构在游戏中的简单应用
- windowsx.h 和windows.h的区别
- NVIDIA的PhysX GPU物理加速概述
- Visual Studio安装卸载模板
- 开博惯例来一篇
- 堆和栈