hdu1251(Trie树)
传送门:统计难题
分析:Trie树入门题,随便写写练下手感,统计每个节点被多少单词经过就可以了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define LL long long
#define N 500010
using namespace std; char s[];
struct Trie
{
int cnt[N],child[N][];
int L,root;
int newnode()
{
memset(child[L],,sizeof(child[L]));
return L++;
}
void init()
{
L=;
memset(cnt,,sizeof(cnt));
root=newnode();
}
void insert(char *s)
{
int now=root;
for(int i=;s[i];i++)
{
int id=s[i]-'a';
if(!child[now][id])
child[now][id]=newnode();
now=child[now][id];
cnt[now]++;
}
}
int query(char *s)
{
int now=root;
for(int i=;s[i];i++)
{
int id=s[i]-'a';
if(!child[now][id])return ;
now=child[now][id];
}
return cnt[now];
}
}trie;
int main()
{
trie.init();
while(gets(s))
{
if(strlen(s)==)break;
trie.insert(s);
}
while(scanf("%s",s)==)printf("%d\n",trie.query(s));
}
hdu1251(Trie树)的更多相关文章
- HDU1251 统计难题 【trie树】
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU1251 统计难题[map的应用][Trie树]
一.题意 给出一组单词,另给出一组单词用作查询,求解对于每个用于查询的单词,前一组中有多少个单词以其为前缀. 二.分析 根据题目很容易想到hash的方法,首先可以朴素的考虑将第一组中的所有单词的前缀利 ...
- Trie树入门及训练
什么叫Trie树? Trie树即字典树. 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本 ...
- 蒟蒻的trie树专题
POJ 3630 Phone List: 模板 ///meek #include<bits/stdc++.h> using namespace std; using namespace s ...
- 【字符串算法】字典树(Trie树)
什么是字典树 基本概念 字典树,又称为单词查找树或Tire树,是一种树形结构,它是一种哈希树的变种,用于存储字符串及其相关信息. 基本性质 1.根节点不包含字符,除根节点外的每一个子节点都包含一个字符 ...
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 洛谷P2412 查单词 [trie树 RMQ]
题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个单词表,包含N个单词 ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之1024求和
题目 解决代码及点评 /* 已知有N个无规律的正整数,请编程序求出其中的素数并打印出能被5整除的数之积. */ #include <stdio.h> # ...
- Swift - 闭包的介绍及用法(以数组排序为例)
闭包(即一些小的匿名代码块),可以像函数一样使用.可以很方便的将闭包传给其他函数,告诉它们应当如何执行某一个任务. 1,使用sort方法和闭包进行数组排序 sort方法返回一个数组的有序版本.(sor ...
- Control.Invoke和Control.BeginInvoke
问题的引入 下面有个简单的demo,大家一看代码就知道效果如何示例.我新建一个winform的程序,然后写入了如下代码: using System; using System.Windows.Form ...
- FZOJ2111:Min Number
Problem Description Now you are given one non-negative integer n in 10-base notation, it will only c ...
- asp.net笔试题
1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...
- String 中的秘密
Navigation: 数据类型相关 > Delphi 的字符及字符串 > [3] - String 中的秘密 //String 的指针地址及实际的内存地址 var str: str ...
- 基于visual Studio2013解决面试题之0207单词翻转
题目
- C++,Python,Go对照学习-02
main函数 Go中有且只有一个main函数,而且main函数必须在package main当中.main函数无返回值也无参数,如果希望获取从命令行传递的参数有其他包解决这个问题. ...
- _00024 尼娜抹微笑伊拉克_云计算ClouderaManager以及CHD5.1.0群集部署安装文档V1.0
笔者博文:妳那伊抹微笑 itdog8 地址链接 : http://www.itdog8.com(个人链接) 博客地址:http://blog.csdn.net/u012185296 博文标题:_000 ...
- Network Panel说明
Chrome Developer Tools:Network Panel说明 官方资料:Chrome Developer Tools: Network Panel 一.chrome Develop ...