统计难题

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 14434    Accepted Submission(s): 6219

Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
 
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
 
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
 
Sample Input
banana
band
bee
absolute
acm
ba
b
band
abc
 
Sample Output
2
3
1
0
 
Author
Ignatius.L
 
Recommend
Ignatius.L
     字典树.....
代码:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. typedef struct trie
  6. {
  7. //由于只有小写字母a~z-->26
  8. struct trie *child[];
  9. int deep; //--->相似的程度
  10. }Trie;
  11.  
  12. /*作为一个头指针*/
  13.  
  14. Trie *root;
  15.  
  16. void Insert(char *a)
  17. {
  18. int len,i;
  19. Trie *current, *creatnew;
  20. len=strlen(a);
  21. if(len)
  22. {
  23. current=root;
  24. for(i=;i<len;i++)
  25. {
  26. if(current->child[a[i]-'a']!=)
  27. {
  28. current=current->child[a[i]-'a'];
  29. current->deep=current->deep+;
  30. }
  31. else
  32. {
  33. creatnew=(Trie *)malloc(sizeof(Trie));
  34. for(int j=;j<;j++)
  35. {
  36. creatnew->child[j]=;
  37. }
  38. current->child[a[i]-'a']=creatnew;
  39. current=creatnew;
  40. current->deep=;
  41. }
  42. }
  43. }
  44. }
  45.  
  46. int Triefind(char *a)
  47. {
  48. int i,len;
  49. Trie *current;
  50. len=strlen(a);
  51. if(!len) return ;
  52. current=root;
  53. for(i=;i<len;i++)
  54. {
  55. if(current->child[a[i]-'a']!=)
  56. {
  57. current=current->child[a[i]-'a'];
  58. }
  59. else
  60. return ;
  61. }
  62. return current->deep;
  63. }
  64.  
  65. int main()
  66. {
  67. int i;
  68. char temp[];
  69. root=(Trie *)malloc(sizeof(Trie));
  70. for(i=;i<;i++)
  71. {
  72. root->child[i]=;
  73. }
  74. root->deep=;
  75. while(gets(temp),strcmp(temp,""))
  76. Insert(temp);
  77. memset(temp,'\0',sizeof(temp));
  78. while(~scanf("%s",temp))
  79. printf("%d\n",Triefind(temp));
  80. free(root);
  81. return ;
  82. }

模板:

代码:

  1. /*hdu 1251 字典树之统计*/
  2. #define LOCAL
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<cstdlib>
  6. typedef struct Trie
  7. {
  8. struct Trie *child[];
  9. int deep;
  10. }trie;
  11.  
  12. int idx(char s){
  13. return s-'a';
  14. }
  15. void Insert(char *s,trie *root)
  16. {
  17. trie *cur,*newcur;
  18. cur=root;
  19. int i,j;
  20. for(i=;s[i]!='\0';i++)
  21. {
  22. if(cur->child[idx(s[i])]==NULL) //为空指针
  23. {
  24. newcur=(trie *)malloc(sizeof(trie));
  25. newcur->deep=;
  26. for(j=;j<;j++)
  27. newcur->child[j]=NULL; //设置为空指针
  28. cur->child[idx(s[i])]=newcur;
  29. }
  30. cur=cur->child[idx(s[i])]; //向下推一层
  31. cur->deep+=; //层数加一
  32. }
  33. }
  34. int query(char *s,trie *root)
  35. {
  36. trie *cur=root;
  37. int i;
  38. for(i=;s[i]!='\0';i++){
  39. if(cur->child[idx(s[i])]!=NULL)
  40. cur=cur->child[idx(s[i])];
  41. else
  42. return ;
  43. }
  44. int tem=cur->deep;
  45. return tem;
  46. }
  47. void del(trie *root)
  48. {
  49. int i=;
  50. for(i=;i<;i++){
  51. if(root->child[i]!=NULL)
  52. del(root->child[i]);
  53. }
  54. free(root);
  55. }
  56. char ss[];
  57. int main()
  58. {
  59. #ifdef LOCAL
  60. freopen("test.in","r",stdin);
  61. #endif
  62. trie *root=(trie *)malloc(sizeof(trie));
  63. for(int i=;i<;i++)
  64. root->child[i]=NULL; //设置为空指针
  65. while(gets(ss),strcmp(ss,""))
  66. Insert(ss,root);
  67. while(scanf("%s",ss)!=EOF)
  68. printf("%d\n",query(ss,root));
  69. del(root);
  70. return ;
  71. }

HDUOJ-----(1251)统计难题的更多相关文章

  1. hduoj 1251 统计难题

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  2. hdu 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    M ...

  3. HDU 1251 统计难题 (Trie)

    pid=1251">统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/ ...

  4. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  5. HDU 1251 统计难题(Trie模版题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  6. HDU 1251统计难题

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  7. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. hdu 1251 统计难题 trie入门

    统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...

  9. [ACM] hdu 1251 统计难题 (字典树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

  10. HDU 1251 统计难题 (字符串-Trie树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

随机推荐

  1. C++常用排序法、随机数

    C++常用排序法研究 2008-12-25 14:38 首先介绍一个计算时间差的函数,它在<time.h>头文件中定义,于是我们只需这样定义2个变量,再相减就可以计算时间差了. 函数开头加 ...

  2. Minimum Window Substring leetcode java

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  3. 【Gson】简介 文档 基本使用 示例

    简介 new TypeToken<List<Person>>() {}.getType() 1 1   1 new TypeToken<List<Person> ...

  4. Java中浮点类型的精度问题 double float

    要说清楚Java浮点数的取值范围与其精度,必须先了解浮点数的表示方法与浮点数的结构组成.因为机器只认识01,你想表示小数,你要机器认识小数点这个东西,必须采用某种方法.比如,简单点的,float四个字 ...

  5. 记录C#错误日志工具

    在编程过程中,我们经常会用try...catch处理可能出错的代码块.如果程序出现错误,则直接show出错误信息. 当然,大型的系统都有错误日志处理模块,用数据库记录错误日志信息,有相应的写入错误日志 ...

  6. 深入理解JSON

    一.JS判断字符串是否为JSON的方法: function isJSON(str) { if (typeof str == 'string') { try { JSON.parse(str); ret ...

  7. IOS Xib使用——为控制器添加Xib文件

    Xib文件是一个轻量级的用来描述局部界面的文件,它与StoryBoard类似,都是使用Interface Bulider工具进行编辑.但是StoryBoard是重量级的,它是用来描述整个软件的多个界面 ...

  8. 交叉编译git

    git依赖openssl.zlib. 首先编译openssl ./Configure linux-armv4 shared 修改Makefile,CC.RANLIB.MAKEDEPPROG为对应的交叉 ...

  9. Smack 结合 Openfire服务器,建立IM通信,发送聊天消息

    在文章开始,请你了解和熟悉openfire方面的相关知识,这样对你理解下面代码以及下面代码的用途有很好的了解.同时,你可能需要安装一个简单的CS聊天工具,来测试你的代码是否成功的在openfire服务 ...

  10. 解决Windows7 Embedded连接手机问题

    故障现象:正确安装厂商自带的驱动后,插入安卓或iPhone手机,提示找到新硬件,却无法成功安装驱动.在此可以肯定的是:手机驱动无问题,手机.连接线也无问题.看来问题又落到“Embedded”上了! 仔 ...