HDU--1251 -- 统计难题 [字典树基本应用] [用数组事先分配空间以节约时间]
来源:互联网 发布:遗传算法优化svm参数 编辑:程序博客网 时间:2024/06/01 15:51
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 13656 Accepted Submission(s): 5858
Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
bananabandbeeabsoluteacmbabbandabc
Sample Output
2310
Code:
啦啦啦~尝试了一下用数组事先分配空间, 比malloc快好多呢
对&符号又加深了理解~~
完胜~~O(∩_∩)O哈哈~
#include"stdio.h"#include"string.h"#include"iostream"#define null 0using namespace std;typedef struct trie{int count;trie *child[26];}trie_node;trie_node memory[1000000];int index = 1;trie_node *tree;void trie_insert(char c[]){int i,j;trie_node *p;p = tree;for(i=0;i<strlen(c);i++){if(p->child[c[i]-'a']){p->child[c[i]-'a']->count++;p = p->child[c[i]-'a'];}else{p->child[c[i]-'a'] = &memory[index++];p->child[c[i]-'a']->count=1;for(j=0;j<26;j++)p->child[c[i]-'a']->child[i] = NULL;p = p->child[c[i]-'a'];}}}int trie_count(char c[]){trie_node *p;p = &memory[1];int i,j,count=0;for(i=0;i<strlen(c);i++){if(!p->child[c[i]-'a']) return 0;else{count = p->child[c[i]-'a']->count;p = p->child[c[i]-'a'];}}return count;}int main(){int i;char str[11];tree = &memory[index++];tree->count=0;for(i=0;i<26;i++)tree->child[i] = NULL;while(gets(str),strcmp(str,""))trie_insert(str);//printf("~~~~~\n");while(gets(str))printf("%d\n",trie_count(str));return 0;}
- HDU--1251 -- 统计难题 [字典树基本应用] [用数组事先分配空间以节约时间]
- HDU 1251-统计难题-基本字典树
- 【字典树模板(数组实现)】HDU 1251 统计难题
- hdu 1251-统计难题(字典树||map||数组)
- hdu-1251统计难题-字典树的简单应用
- HDU 1251 统计难题 字典树
- HDU 1251统计难题(字典树)
- [字典树 ]hdu 1251 统计难题
- 统计难题 hdu 1251 字典树
- hdu 1251 统计难题 字典树
- HDU 1251 统计难题 -- 字典树
- HDU 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu-1251-统计难题(字典树)
- hdu 1251 统计难题 (字典树)
- HDU 1251统计难题 字典树
- plsql-聚簇表初解
- libnl3安装及使用
- A Security-focused HTTP Primer---http://danielmiessler.com/popular/
- VC++无标题窗口以及控件拖动的方法(修正)
- 怎样学会理财?
- HDU--1251 -- 统计难题 [字典树基本应用] [用数组事先分配空间以节约时间]
- CodeForces 178B1 - Greedy Merchants tarjan求双联通分量+tarjan离线求最近公共祖先
- ibasefilter 转
- mysql的安装目录下找不到data目录的解决方法
- Apache Pig的一些基础概念及用法总结(1)
- mssql查询特定表&列是否存在
- poj 3414 搜索
- fun((exp1,exp2),(exp3,exp4,exp5))有几个实参(关于逗号表达式)?
- 第一篇博客。。