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

代码:

  #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define INF 0x7fffffff int ch[1000000][30],isword[1000000];
int num[1000000],nz; void insert(char s[],int len){
int i,j,u = 0;
num[u]++;
for(i=0;i<len;i++){
int v = s[i] - 'a' ;
if(!ch[u][v]){
memset(ch[nz],0,sizeof(ch[nz]));
isword[nz] = 0;
ch[u][v] = nz ++ ;
}
u = ch[u][v] ;
num[u]++;
}
isword[u] = 1;
} int query(char s[]){
int i,j,len,u = 0;
len = strlen(s);
for(i=0;i<len;i++){
int v = s[i] - 'a';
if(!ch[u][v]){
return 0;
}
u = ch[u][v];
}
return num[u];
} int main(){
int i,len;
char s[12],c;
nz = 1;
memset(num,0,sizeof(num));
memset(ch[0],0,sizeof(ch[0]));
c = getchar();
while(c != '\n'){
gets(s+1);
s[0] = c ;
len = strlen(s);
s[len] = '\0' ;
insert(s,len);
c = getchar();
} while(scanf("%s",s) == 1){
int ans = query(s);
printf("%d\n",ans);
}
return 0;
}

HDU 1251 字典树(前缀树)的更多相关文章

  1. 9-11-Trie树/字典树/前缀树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第9章  查找 - Trie树/字典树/前缀树(键树) ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版>(严蔚 ...

  2. hdu 1251(字典树)

    题目链接:http://acm.hdu.edu.cn/status.php?user=NYNU_WMH&pid=1251&status=5 Trie树的基本实现 字母树的插入(Inse ...

  3. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. [LeetCode] 208. Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

  5. 内存空间有限情况下的词频统计 Trie树 前缀树

    数据结构与算法专题--第十二题 Trie树 https://mp.weixin.qq.com/s/nndr2AcECuUatXrxd3MgCg

  6. HDU - 1251 字典树模板题

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

  7. hdu 1251 字典树的应用

    这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include < ...

  8. hdu 1251 字典树模板题 ---多串 查找单词出现次数

    这道题题目里没有给定数据范围 我开了2005  疯狂的WA 然后开了50000, A掉  我以为自己模板理解错  然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...

  9. Trie - leetcode [字典树/前缀树]

    208. Implement Trie (Prefix Tree) 字母的字典树每个节点要定义一个大小为26的子节点指针数组,然后用一个标志符用来记录到当前位置为止是否为一个词,初始化的时候讲26个子 ...

随机推荐

  1. 移动互联与O2O的完美衔接

    移动互联网虽然市场颇大,前景广阔,但是由于数据过于密集,很难精准的定位所谓的目标客户群,然而O2O的线下市场却与互联网市场有极大的反差.一直觉得高校周边的小商家是最幸福的生意人,客户明确(就是本校学生 ...

  2. redis 学习笔记三(队列功能)

    Redis队列功能介绍 List 常用命令: Blpop删除,并获得该列表中的第一元素,或阻塞,直到有一个可用 Brpop删除,并获得该列表中的最后一个元素,或阻塞,直到有一个可用 Brpoplpus ...

  3. [置顶] android之存储篇_SQLite数据库_让你彻底学会SQLite的使用

    SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么. 例如:可以在Integer类型的字段中存放字符串,或者在布尔型字段中存放浮点数,或者在字符型字段中 ...

  4. global.asax?app.config?webconfig??

    一.Global.asax 1.global.asax是什么? 一个文本文件,至于它包含写什么内容?顾名思义,global 肯定是掌管一个应用程序(application)的全局性的东西,例如应用程序 ...

  5. Ext中窗体第二次点击报错或者其内控件不显示的问题,弄了2天才解决,记录下

    registerPanel.js: registerPanel = new Ext.form.FormPanel({ id:'registerPanel', layout:'form', autoHe ...

  6. c语言筛选质数

    #include <stdio.h> #include <stdlib.h> #include <math.h> int isit(int num) { int i ...

  7. Java中的enum

    package com.st.java; /** * ENUM枚举类型的使用 * @author Administrator * 2016年04月10日 */ public enum MoneyTyp ...

  8. hdu 4586 Play the Dice(概率dp)

    Problem Description There is a dice with n sides, which are numbered from 1,2,...,n and have the equ ...

  9. html li标签前面添加图标三种方法

    今天无聊写下这个例子,希望对初学者有帮助,代码如下 <!DOCTYPE html> <html> <head> <meta charset="utf ...

  10. css中的字体样式及元素样式

    css中的字体样式一般包含有就9中,常见的有7种.这7种依次为: 1.字体样式:font-family: 2.字体大小:font-size: 3.字体加粗:font-weight: 4.字体斜体:fo ...