HDU1251——统计难题(字典树模板)
来源:互联网 发布:恋爱软件免费版 编辑:程序博客网 时间:2024/05/22 12:11
最基础的模板,统计前缀。
#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <limits.h>#include <math.h>#include <algorithm>using namespace std;const int MAX=26;typedef struct TrieNode{ int nCount; // 该节点前缀 出现的次数 struct TrieNode *next[MAX]; //该节点的后续节点} TrieNode;TrieNode Memory[1000000]; //先分配好内存。 malloc 较为费时int allocp = 0;//初始化一个节点。nCount计数为1, next都为nullTrieNode * createTrieNode(){ TrieNode * tmp = &Memory[allocp++]; tmp->nCount = 1; for (int i = 0; i < MAX; i++) tmp->next[i] = NULL; return tmp;}void insertTrie(TrieNode * * pRoot, char * str){ TrieNode * tmp = *pRoot; int i = 0, k; //一个一个的插入字符 while (str[i]) { k = str[i] - 'a'; //当前字符 应该插入的位置 if (tmp->next[k]) { tmp->next[k]->nCount++; } else { tmp->next[k] = createTrieNode(); } tmp = tmp->next[k]; i++; //移到下一个字符 }}int searchTrie(TrieNode * root, char * str){ if (root == NULL) return 0; TrieNode * tmp = root; int i = 0, k; while (str[i]) { k = str[i] - 'a'; if (tmp->next[k]) { tmp = tmp->next[k]; } else return 0; i++; } return tmp->nCount; //返回最后的那个字符 所在节点的 nCount}int main(void){ char s[11]; TrieNode *Root = createTrieNode(); while (gets(s) && s[0] != '\0') //读入0 结束 { insertTrie(&Root, s); } while (gets(s)) //查询输入的字符串 { printf("%d\n", searchTrie(Root, s)); } return 0;}
0 0
- HDU1251——统计难题(字典树模板)
- HDU1251 统计难题(字典树模板)
- HDU1251统计难题(字典树模板)
- hdu1251统计难题—简单字典树
- hdu1251-统计难题-字典树模板题
- 字典树模板 hdu1251统计难题
- hdu1251统计难题-字典树模板题
- C++——字典树(Trie树)模板——统计难题(HDU1251)
- hdu1251 统计难题(字典树)
- hdu1251统计难题(字典树)
- hdu1251 统计难题(字典树)
- hdu1251 统计难题(字典树)
- hdu1251统计难题(字典树模版)
- HDU1251 统计难题(字典树)
- 统计难题(hdu1251,字典树)
- hdu1251 统计难题 (字典树查找)
- hdu1251统计难题(字典树)
- hdu1251 统计难题(字典树)
- URL
- 浅析CVE-2012-0056
- 采用不同方式进行数值交换
- 直方图匹配
- Linux下Wi-Fi的实现:wireless_tools和wpa_supplicant
- HDU1251——统计难题(字典树模板)
- 解决编译Apache出现的问题:configure: error: APR not found . Please read the documentation
- 为什么Objective-C中有很多类是以NS、UI开头的
- DNS 开源DNS之dnspod-sr介绍
- declare-styleable中format详解
- AFNetWorking(持续更新)
- hdu 1055 color on a tree
- iwpriv 配置STA连接选项命令
- Linux学习笔记(三)