HDU_1251_统计难题(字典树)
来源:互联网 发布:网络通信公司简介 编辑:程序博客网 时间:2024/05/29 02:22
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 20365 Accepted Submission(s): 8878
Total Submission(s): 20365 Accepted Submission(s): 8878
Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
bananabandbeeabsoluteacmbabbandabc
Sample Output
2310
解析:利用字典树求给定公共前缀的字符串个数。(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;}
0 0
- HDU_1251_统计难题(字典树)
- hdu_1251_统计难题_字典树
- 统计难题(字典树)
- 统计难题(字典树)
- 统计难题 (字典树)
- 统计难题(字典树)
- 统计难题【字典树】
- 统计难题 字典树
- 统计难题(字典树)
- hdu1251 统计难题(字典树)
- HDU 1251统计难题(字典树)
- 统计难题(字典树的应用)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu1251统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 统计难题(字典树)
- 统计难题(字典树 模版题)
- Mybatis简单测试
- json 在文件main函数,junit中可以用 但是在servelt中不可以用
- 关于两列布局和三列布局
- 第一篇博客
- Customers Who Never Order
- HDU_1251_统计难题(字典树)
- The Most Simple Introduction to Hypothesis Testing
- nachos priority scheduler with nonation
- power of the test
- Is the “*apply” family really not vectorized?
- POJ_2513_Colored Sticks(欧拉路+字典树)
- How to distinguish between strings in heap or literals?
- 深入理解内存地址
- Remove element