zoukankan      html  css  js  c++  java
  • 统计难题 HDU

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

    注意:本题只有一组测试数据,处理到文件结束.
    Output对于每个提问,给出以该字符串为前缀的单词的数量.
    Sample Input

    banana
    band
    bee
    absolute
    acm
    
    ba
    b
    band
    abc

    Sample Output

    2
    3
    1
    0

    学习地址:
    http://blog.csdn.net/u013480600/article/details/44673031
    http://blog.csdn.net/hackbuteer1/article/details/7964147
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 using namespace std;
     6 
     7 struct node{
     8     int num;
     9     node* next[26];
    10     node(){
    11         num=0;
    12         memset(next,0,sizeof(next));
    13     }
    14 };
    15 
    16 node* root=new node();
    17 node* rt;
    18 
    19 int id,len;
    20 
    21 void build(char str[30]){
    22     rt=root;
    23     len=strlen(str);
    24     for(int i=0;i<len;i++){
    25         id=str[i]-'a';
    26         if(rt->next[id]==NULL) rt->next[id]=new node();
    27         rt=rt->next[id];
    28         rt->num++;
    29     }
    30 }
    31 
    32 int querry(char str[30]){
    33     rt=root;
    34     len=strlen(str);
    35     for(int i=0;i<len;i++){
    36         id=str[i]-'a';
    37         if(rt->next[id]==NULL) return 0;
    38         rt=rt->next[id];
    39     }
    40     return rt->num;
    41 }
    42 
    43 int main()
    44 {   char str[30];
    45     while(gets(str)&&str[0]) build(str);
    46     while(gets(str)) printf("%d
    ",querry(str));
    47     return 0;
    48 }
  • 相关阅读:
    @ModelAttribute 与@InitBinder
    springboot开启矩阵传参MatrixVariable
    socket(一)
    request请求《一》
    Ajax请求中的async:false/true的作用
    java.lang.NullPointerException at org.apache.jsp.index_jsp._jspInit(index_jsp.java:40)
    shiro登录源码
    js(正则验证)
    多进程之间的通信
    队列中常用方法的使用
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6852345.html
Copyright © 2011-2022 走看看