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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251

    统计难题

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


    Problem Description
    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
     
    Input
    输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

    注意:本题只有一组测试数据,处理到文件结束.
     
    Output
    对于每个提问,给出以该字符串为前缀的单词的数量.
     
    Sample Input
    banana
    band
    bee
    absolute
    acm
     
    ba
    b
    band
    abc
     
    Sample Output
    2
    3
    1
    0
     
    题目大意:中文题。找到相同前缀的有多少个。
    解题思路:建树,字典树的模板题目。
     
    详见代码。
    第一种用Map。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <map>
     4 #include <cstring>
     5 #include <string>
     6 
     7 using namespace std;
     8 
     9 char str[110];
    10 string ss;
    11 char ch1[110],ch[110];
    12 
    13 int main()
    14 {
    15     map<string,int>qq;
    16     //qq[string]=int;
    17     while (gets(str))
    18     {
    19         int len=strlen(str);
    20         if (len==0)
    21             break;
    22         int j;
    23         for (int i=0;i<len;i++)
    24         {
    25             for (j=0;j<=i;j++)
    26             {
    27                 ch1[j]=str[j];
    28             }
    29             ch1[j]='';
    30             ss.assign(ch1);
    31             qq[ss]++;
    32         }
    33     }
    34     while (~scanf("%s",&ch))
    35     {
    36         //cout<<1111111<<endl;
    37         cout<<qq[ch]<<endl;
    38     }
    39     return 0;
    40 }

    第二种,字典树。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 struct node
    10 {
    11     int Count;
    12     node *next[26];
    13     node() //初始化数据
    14     {
    15         for (int i=0;i<26;i++)
    16             next[i]=NULL;
    17         Count=0;
    18     }
    19 };
    20 
    21 node *p,*root=new node();
    22 void Insert(char *s)//插入新单词,即建立字典树
    23 {
    24     int i,k;
    25     p=root;
    26     for (i=0; s[i]!=''; i++)
    27     {
    28         k=s[i]-'a';
    29         if (p->next[k]==NULL)
    30             p->next[k]=new node();//判断是不是新节点,如果是分配创建一个新节点来存贮 ,即root的next域对应的k位置是否为空
    31         p=p->next[k];
    32         p->Count++; //记录此字母出现的次数
    33     }
    34 }
    35 
    36 int Search(char *s)
    37 {
    38     int i,k;
    39     p=root;
    40     for (i=0; s[i]!=''; i++)
    41     {
    42         k=s[i]-'a';
    43         if (p->next[k]==NULL)//一旦查找不到,立即跳出
    44             return 0;
    45         p=p->next[k];
    46     }
    47     /*if (s[i]!='')//s[i]!=0表示中间
    48         return 0;*/
    49     return p->Count;
    50 }
    51 
    52 int main()
    53 {
    54     char s[11];
    55     while (gets(s))
    56     {
    57         int len=strlen(s);
    58         if (len==0)
    59             break;
    60         Insert(s);
    61     }
    62     while (gets(s))
    63     {
    64         printf ("%d
    ",Search(s));
    65     }
    66     return 0;
    67 }
     
  • 相关阅读:
    Bootstrap 可视化布局--拖拽后弹窗进行编辑
    Missing letters
    DNA Pairing
    Pig Latin
    Search and Replace
    Where art thou
    Roman Numeral Converter
    Diff Two Arrays
    Sum All Numbers in a Range
    Caesars Cipher
  • 原文地址:https://www.cnblogs.com/qq-star/p/4750544.html
Copyright © 2011-2022 走看看