HDU_1251_统计难题(字典树)
2015-03-24 01:59
399 查看
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 20365 Accepted Submission(s): 8878
Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
banana band bee absolute acm ba b band abc
Sample Output
2 3 1 0
解析:利用字典树求给定公共前缀的字符串个数。(ps:第一次写字典树,还是用的数组,给跪了)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251
代码清单:
#include<map> #include<cmath> #include<stack> #include<queue> #include<ctime> #include<cctype> #include<string> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; struct edge{ int num; int letter[26]; }trie[500000]; char s[11]; int number=0; void InitTrie(){ for(int i=0;i<500000;i++){ trie[i].num=0; memset(trie[i].letter,0,sizeof(trie[i].letter)); } } void creatTrie(char s[]){ int j=0; int len=strlen(s); for(int i=0;i<len;i++){ if(trie[j].letter[s[i]-'a']){ j=trie[j].letter[s[i]-'a']; trie[j].num++; } else{ trie[j].letter[s[i]-'a']=++number; j=number; trie[j].num=1; } } } int findTrie(char s[]){ int j=0; int len=strlen(s); for(int i=0;i<len;i++){ if(trie[j].letter[s[i]-'a']){ j=trie[j].letter[s[i]-'a']; } else return 0; } return trie[j].num; } int main(){ InitTrie(); //freopen("liuchu.txt","r",stdin); while(gets(s)&&s[0]!='\0') creatTrie(s); while(scanf("%s",s)!=EOF){ printf("%d\n",findTrie(s)); } return 0; }
续:指针操作字典树(C++才过的)
代码清单:
#include<map> #include<cmath> #include<stack> #include<queue> #include<ctime> #include<cctype> #include<string> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int MAX = 26; struct trie{ trie *next[MAX]; int v; }; trie *root=new trie(); char s[15]; void ctreatTrie(char *s){ trie *p=root, *q; int len=strlen(s), pos; for(int i=0;i<len;i++){ pos=s[i]-'a'; if(p->next[pos]==NULL){ q=new trie(); q->v=1; for(int j=0;j<MAX;j++) q->next[j]=NULL; p->next[pos]=q; p=p->next[pos]; } else{ p->next[pos]->v++; p=p->next[pos]; } } } int findTrie(char *s){ trie *p=root; int len=strlen(s), pos; for(int i=0;i<len;i++){ pos=s[i]-'a'; if(p->next[pos]==NULL) return 0; p=p->next[pos]; } return p->v; } void delTrie(trie *Root){ for(int i=0;i<MAX;i++){ if(Root->next[i]) delTrie(Root->next[i]); } free(Root); } int main(){ //freopen("liuchu.txt","r",stdin); for(int i=0;i<MAX;i++) root->next[i]=NULL; while(gets(s)&&s[0]!='\0') ctreatTrie(s); while(scanf("%s",s)!=EOF) printf("%d\n",findTrie(s)); delTrie(root); return 0; }
相关文章推荐
- [字典树 ]hdu 1251 统计难题
- HDU 1251 统计难题 字典树
- HDU 1251 统计难题 -- 字典树
- HDU--1251 -- 统计难题 [字典树基本应用] [用数组事先分配空间以节约时间]
- hdu1251 统计难题 字典树
- HDU 1251统计难题(字典树)
- HDU 1251统计难题 字典树
- hdu 1251 统计难题(字典树)
- HDU 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- 统计难题 hdu 1251 字典树
- HDU 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- HDU 1251 统计难题 字典树入门(Trie)
- HDU 1251 统计难题(字典树)
- HDU 1251 统计难题 (java版字典树)
- HDU 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题 字典树
- hdu 1251 统计难题(字典树模版)