http://acm.hdu.edu.cn/showproblem.php?pid=1251

题意:
给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量。

思路:

字典树入门题。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = ; int num = ; struct Trie
{
int son[];
int cnt; //前缀数量
int ends; //单词数量,在本题中其实并没有用到
}t[maxn]; void init(int x)
{
t[x].ends = ;
t[x].cnt = ;
memset(t[x].son,,sizeof(t[x].son));
} void insert(char* s)
{
int u = , n = strlen(s);
for(int i=;i<n;i++)
{
int c = s[i]-'a';
if(!t[u].son[c])
{
num++;
init(num);
t[u].son[c] = num;
}
u = t[u].son[c];
t[u].cnt++;
}
t[u].ends++;
} int query(char* s)
{
int u = , n = strlen(s);
for(int i=;i<n;i++)
{
int c = s[i]-'a';
if(t[u].son[c] == ) return ;
u = t[u].son[c];
}
return t[u].cnt;
} char s[]; int main()
{
while(gets(s) && strcmp(s,"")!=) insert(s);
while(scanf("%s",s)!=EOF) printf("%d\n",query(s));
return ;
}

HDU 1251 统计难题(字典树模板题)的更多相关文章

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

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

  2. hdu 1251 统计难题 字典树第一题。

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

  3. HDU 1251 统计难题 字典树大水题

    今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...

  4. hdu -1251 统计难题(字典树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...

  5. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

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

  6. HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

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

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

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

  8. HDU 1251 统计难题(字典树)

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

  9. HDU 1251统计难题 字典树

    字典树的应用. 数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊.赶紧来熟悉一下字典树. 空间开销太大T T #include<cstdio> #include<cstrin ...

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

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

随机推荐

  1. 迭代器 生成器 yield

    iter 迭代iterable 可迭代的 iterator迭代器 dir函数查看一个数据类型内部含有哪些方法 两边带着双下划线的方法叫做"魔术方法","双下方法" ...

  2. [转载]SQL中EXISTS的用法

    比如在Northwind数据库中有一个查询为SELECT c.CustomerId,CompanyName FROM Customers cWHERE EXISTS(SELECT OrderID FR ...

  3. springboot+mybaties

    1. 开发工具:Intellij idea2018.3 2. file----->new----->project 3. 选择Spring Initializr,点击next 4. 点击n ...

  4. self asyncio

    import asyncio from threading import Thread import time print('main start:',time.time()) async def d ...

  5. Docker学习笔记之常用的 Docker Compose 配置项

    0x00 概述 与 Dockerfile 一样,编写 Docker Compose 的配置文件是掌握和使用好 Docker Compose 的前提.编写 Docker Compose 配置文件,其本质 ...

  6. Centos环境自写脚本查看使用php或nginx占用内存

    在CentOs6.4下,用root权限测试. # cd ~ //进入home目录 # vim .bashrc //编辑文件,把下面代码放入地址 mem () { top -n1 -b | head - ...

  7. ThirdAPI

    //public class ThirdAPI //{ // [DllImport("ThirdAPI.dll")] // public static extern int Ini ...

  8. 【题解】bzoj 4478 [Jsoi2013]侦探jyy

    原题传送门 弱智搜索题 我们就枚举每个点,先判断它是否必须发生,如果没有必须发生,开始搜索它的祖先,如果祖先中有必须发生的,那么它就必须发生,如果祖先中没有必须发生的,那么搜索所有入度为0的点(除了它 ...

  9. Getting started with Kaggle -- Kaggle Competitions

    1: The Competition We'll be learning how to generate a submission for a Kaggle competition. Kaggle i ...

  10. python简说(十三)递归

    #递归就是函数自己调用自己count = 0# def abc():# pass# abc()最多循环999次