字典树模板 HDU
来源:互联网 发布:太极熊猫 mac 编辑:程序博客网 时间:2024/05/16 14:01
字典树模板分为动态分配内存的和数组的写法。动态分配内存的在多组数据的情况下,要销毁内存,很麻烦。建议用数组的。
#include <iostream>#include <stdio.h>#include <algorithm>#include <cstring>#include <queue>using namespace std;#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node{ int next[27]; int v; void init(){ v=0; memset(next,-1,sizeof(next)); }};struct node L[1000000];int tot=0;void add(char a[],int len){ int now=0; for(int i=0;i<len;i++){ int tmp=a[i]-'a'; int next=L[now].next[tmp]; if(next==-1){ next=++tot; L[next].v=-1; L[next].init(); L[now].next[tmp]=next; } now=next; L[now].v++; }}int query(char a[],int len){ int now=0; for(int i=0;i<len;i++){ int tmp=a[i]-'a'; int next=L[now].next[tmp]; if(next==-1)return 0; now=next; } return L[now].v;}int main(){ char str[20]; L[0].init(); while(gets(str),strcmp(str,"")) add(str,strlen(str)); while(gets(str)!=NULL) printf("%d\n",query(str, strlen(str))); return 0;}
阅读全文
0 0
- 字典树模板 HDU
- hdu 1305 字典树 模板
- hdu 1671 字典树 模板
- HDU 1251 字典树裸模板
- 字典树模板--静态 ----HDU 1251
- HDU 1671 Phone List 字典树模板
- HDU 1251 统计难题(字典树模板)
- hdu 1251 统计难题 字典树解释 +字典树模板
- 【字典树模板(数组实现)】HDU 1251 统计难题
- HDU 1671 Phone List (字典树模板)
- hdu 1251 (字典树 Trie 模板题)
- HDU - 1251 统计难题(字典树模板题)
- HDU 1251 统计难题(字典树模板题)
- HDU 1251 (统计难题) 字典树模板&&map实现
- HDU 1251 统计难题 【动态存储+字典树(模板)】
- HDu-1247 Hat’s Words,字典树裸模板!
- HDU 1251 统计难题(字典树模板)
- HDU 1671 Phone List(字典树模板)
- 给定一个整数N,那么N的阶乘N!末尾有多少个0呢?
- 关于传入不同参数请求同一Ajax后返回数据显示错乱的问题
- VMware12 pro装unlocker207补丁后依然没有apple mac选项,问题解决
- zookeeper(1) linux下的安装
- 仿遥控器菜单圆盘
- 字典树模板 HDU
- Maven的搭建
- ORACLE基本数据类型总结
- 算法题1:字符序列交换(阿里巴巴笔试题)
- 解析数组
- java解析任意形式的json,树型结构展示
- 【HAOI2009】毛毛虫
- JAVA中堆和栈的区别
- fabric源码解析6——GRPC服务