hdu1251 字典树
来源:互联网 发布:网络会员制营销 编辑:程序博客网 时间:2024/04/30 13:20
统计前缀,模板的字典树~
#include<stdio.h>#include<stdlib.h>#include<string.h>using namespace std;struct node{ int cnt; node *next[26];}*root;node *build(){ node *p=(node *)malloc(sizeof(node)); for(int i=0;i<26;i++) p->next[i]=NULL; p->cnt=1;//初始化应该是1。。 return p;}void insert(char *word)//构造字典树{ int len=strlen(word); node *p; p=root; for(int i=0;i<len;i++) { if(p->next[word[i]-'a']==NULL) p->next[word[i]-'a']=build(); else p->next[word[i]-'a']->cnt++;//计数 //printf("cnt%d\n",p->next[word[i]-'a']->cnt); p=p->next[word[i]-'a']; }}int query(char *ask){ int ans=0; int len=strlen(ask); node *p; p=root; for(int i=0;i<len;i++) { if(!(p->next[ask[i]-'a'])) return 0; ans=p->next[ask[i]-'a']->cnt;p=p->next[ask[i]-'a']; } return ans;}int main(){ char word[15],ask[15]; root=build(); while(gets(word)&&word[0])//.........跪在输入上,gets是可以有空格的,scanf不可以 { insert(word); } while(scanf("%s",ask)!=EOF) { printf("%d\n",query(ask)); } return 0;}
0 0
- HDU1251字典树
- hdu1251 字典树
- hdu1251之字典树
- hdu1251(字典树)
- hdu1251 字典树
- hdu1251 字典树
- hdu1251(字典树)
- hdu1251 字典树
- 字典树hdu1251
- HDU1251-字典树模版
- hdu1251 字典树
- trie hdu1251 字典树
- 字典树hdu1251
- hdu1251(字典树入门)
- HDU1251(字典树)
- 字典树 hdu1251
- 字典树hdu1251
- hdu1251-字典树
- [编程之美] PSet2.13 子数组的最大乘积
- Hybrid--WebView中使用Ajax
- OC:打僵尸问题(类的问题)
- poj1703
- Highcharts选项配置详细说明文档
- hdu1251 字典树
- Android BaiduMap 定位到指定坐标
- 单例的灾难
- 【黑马程序员】Java学习技术博客——银行业务调度系统
- 从“O2O演唱会”中得到的启示
- 封了博客
- 关于java中的StringBuilder的线程安全问题
- 20 Funny Commands of Linux or Linux is Fun in Terminal
- hdu 4920 Matrix multiplication