zoukankan      html  css  js  c++  java
  • hdu--1251 统计难题(字典树水题)

    Description

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

    Input

    输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 

    注意:本题只有一组测试数据,处理到文件结束. 

    Output

    对于每个提问,给出以该字符串为前缀的单词的数量. 

    Sample Input

    banana
    band
    bee
    absolute
    acm
    
    ba
    b
    band
    abc

    Sample Output

    2
    3
    1
    0
    题意:我就不说了,你懂的^_^
    思路:这是一个简单的字典树模板题
    1 struct note//树的定义
    2 {
    3     note *next[26];//a~z是26,再加A~Z是52,再加0~9是62,
    4     int v;//存储这个节点被使用的次数
    5 };
     1 int ins(char *s)//更新树中的节点
     2 {
     3     note *p,*q;
     4     int len=strlen(s);
     5     if(len==0)
     6         return 0;
     7     p=root;
     8     for(int i=0;i<len;i++)//新建节点
     9     {
    10         int d=s[i]-'a';
    11         if(p->next[d]==0)
    12         {
    13             q=(note *)malloc(sizeof(note));
    14             q->v=1;//新建的节点最开始肯定是被使用过1次的
    15             for(int j=0;j<26;j++)
    16             q->next[j]=0;
    17             p->next[d]=q;
    18             p=q;
    19         }
    20         else//被使用过得节点更新
    21         {
    22             p=p->next[d];
    23             p->v=p->v+1;
    24         }
    25     }
    26     return 0;
    27 }
     1 int sea(char *s)//搜索前缀
     2 {
     3     int len=strlen(s);
     4     if(len==0)
     5         return 0;
     6     note *p,*q;
     7     p=root;
     8     for(int i=0;i<len;i++)
     9     {
    10         int d=s[i]-'a';
    11         p=p->next[d];
    12         if(p==0)
    13             return 0;
    14     }
    15     return p->v;
    16 }

    AC代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cstdlib>
     6 using namespace std;
     7 struct note//树的定义
     8 {
     9     note *next[26];//a~z是26,再加A~Z是52,再加0~9是62,
    10     int v;//存储这个节点被使用的次数
    11 };
    12  struct note *root;
    13 int ins(char *s)//更新树中的节点
    14 {
    15     note *p,*q;
    16     int len=strlen(s);
    17     if(len==0)
    18         return 0;
    19     p=root;
    20     for(int i=0;i<len;i++)//新建节点
    21     {
    22         int d=s[i]-'a';
    23         if(p->next[d]==0)
    24         {
    25             q=(note *)malloc(sizeof(note));
    26             q->v=1;//新建的节点最开始肯定是被使用过1次的
    27             for(int j=0;j<26;j++)
    28             q->next[j]=0;
    29             p->next[d]=q;
    30             p=q;
    31         }
    32         else//被使用过得节点更新
    33         {
    34             p=p->next[d];
    35             p->v=p->v+1;
    36         }
    37     }
    38     return 0;
    39 }
    40 int sea(char *s)//搜索前缀
    41 {
    42     int len=strlen(s);
    43     if(len==0)
    44         return 0;
    45     note *p,*q;
    46     p=root;
    47     for(int i=0;i<len;i++)
    48     {
    49         int d=s[i]-'a';
    50         p=p->next[d];
    51         if(p==0)
    52             return 0;
    53     }
    54     return p->v;
    55 }
    56 int main()
    57 {
    58     root=(note *)malloc(sizeof(note));
    59     for(int i=0;i<26;i++)
    60         root->next[i]=0;
    61     root->v=2;
    62     char s[20];
    63     int flag;
    64     flag=0;
    65     while(gets(s))
    66     {
    67         if(strcmp(s,"")==0)
    68         {
    69             break;
    70         }
    71         else
    72             ins(s);
    73     }
    74     while(gets(s))
    75     {
    76         if(strcmp(s,"")==0)
    77         {
    78             break;
    79         }
    80         else
    81         printf("%d
    ",sea(s));
    82     }
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    设计模式的分类
    设计模式工厂方法模式
    设计模式的定义
    帕斯卡命名法
    C#编写程序找一找一个二维数组中的鞍点(即该位置上的元素值在行中最大,在该 列上最小。有可能数组没有鞍点)。要求:1.二维数组的大小、数组元素的值在运行时输入;2.程序有友好的提示信息。
    设计模式抽象工厂模式
    设计模式七大原则
    理解C#中的接口
    Linux下如何查看CPU信息, 包括位数和多核信息
    关于结构体内存对齐
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/6057512.html
Copyright © 2011-2022 走看看