hdoj 1251 统计难题

统计难题

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

Problem Description

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

Input

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

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

Output

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

Sample Input

banana

band

bee

absolute

acm

ba

b

band

abc

Sample Output

2

3

1

0

照样是一道水题,写随笔还是为了方便自己以后看,直接贴ac代码,对了,提交必须要用c++,不然会内存超限。直接贴ac代码

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
struct tree
{
    tree *child[26];
    int n;
};
void insert(char danci[],tree *root)
{

    tree *ans=root;
    int l=strlen(danci);
    for(int i=0;i<l;i++)
    {
        if(ans->child[danci[i]-‘a‘]==NULL)
        {
            tree *temp;
            temp=(tree*)malloc(sizeof(tree));
            temp->n=1;
            for(int j=0;j<26;j++)
                temp->child[j]=NULL;
            ans->child[danci[i]-‘a‘]=temp;
            ans=temp;
        }
        else
        {
            ans=ans->child[danci[i]-‘a‘];
            ans->n++;
        }
    }
}
void search(char danci[],tree *root)
{
    tree *ans=root;
    int l=strlen(danci);
    int i=0;
    for(;i<l;i++)
    {
        if(ans->child[danci[i]-‘a‘]!=NULL)
        {
            ans=ans->child[danci[i]-‘a‘];
        }
        else break;
    }
    if(i==l) cout<<ans->n<<endl;
    else cout<<0<<endl;
}
int main()
{
    tree *root=(tree *)malloc(sizeof(tree));
    root->n=0;
    for(int i=0;i<26;i++)
        root->child[i]=NULL;
    char danci[12];
    gets(danci);
    while(danci[0]!=‘\0‘)
    {
        insert(danci,root);
        gets(danci);
    }
    while(cin>>danci)
    {
        search(danci,root);
    }
    return 0;
}
时间: 2024-08-06 10:33:56

hdoj 1251 统计难题的相关文章

hdoj 1251 统计难题(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路分析:该问题要求求出以某个字符串为前缀的单词数目,通过使用字典树,在字典树中添加count记录通过该结点的单词数目即可: 查找时找到前缀的最后一个单词的结点的count值即为所求: 代码如下: #include <cstdio> #include <cstring> #include <iostream> using namespace std; const in

hdoj 1251 统计难题(经典字典树)

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 84414    Accepted Submission(s): 31834 Problem Description Contest time again! How excited it is to see balloons floating ar

hdu 1251 统计难题 (map水过)

# include <stdio.h> # include <algorithm> # include <string.h> # include <map> # include <iostream> using namespace std; int main() { char a; string x; map<string,int>q; while(true) { scanf("%c",&a); if(a=

HDU 1251 统计难题 Trie题解

基本上是标准的寻找前缀的问题,只需要insert和search函数就可以了. 我这里主要是修改一下n的记录方法,这里的n代表的不是叶子节点的标志,而是有多少单词经过了这条路径的标志. 然后是查找需要查找的前缀单词,如果没有找到,就返回0,表示没有单词以这个前缀单词为前缀,如果找到,直接返回n就是答案了.因为有n个单词经过了这条路径. 查找效率是常数. 使用静态分配空间的办法. #include <stdio.h> #include <string.h> const int MAX_

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

统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每一个提问都是一个字符串. 注意:本题仅仅有一组測试数据,处理到文件结束. Out

hduoj 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): 21236    Accepted Submission(s): 9145 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成

hdu 1251 统计难题 字典树

// hdu 1251 统计难题 字典树 // // 题目大意: // // 有一系列的单词表,以空行结尾,之后会有一些字母串,找出以这些字符串 // 作为前缀的单词的个数 // // // 解题思路: // // 字典树 Trie,在插入字符串的时候每遇到一个节点,该节点的值++.查找的时候 // 字符串时,如果找到了,那么返回当前的val,否则返回0,因为没有以这个字符串 // 为前缀的单词. // // // 感悟: // // 这段时间想学学数据结构,就看了看刘老的大白书,感觉用数组挺巧

【hdoj】1251 统计难题 【数据结构-Trie树裸题】

传送门:统计难题 题意: 字典树裸题. 分析 字典树板子,但是这题需要注意一点. 关于字典树的只是可以参考hihocoder hiho一下 第二周 用G++提交会爆内存(Memory Limit Exceeded),用c++提交可以AC. G++ 与 C++提交的区别 参考:OJ中的语言选项里G++ 与 C++的区别 C++是一门计算机编程语言,而G++则是C++的编译器. 选择C++意味着你将使用C++最标准的编译方式,也就是ANSI C++编译. 选择G++则意味这你使用GNU项目中适用人群

HDU 1251统计难题

D - 统计难题 Time Limit:2000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1251 Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度