HDOJ1251-统计难题(trie树入门)
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 16945 Accepted Submission(s): 7292
注意:本题仅仅有一组測试数据,处理到文件结束.
banana
band
bee
absolute
acm ba
b
band
abc
2
3
1
0
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 500000;
int ch[maxn][26];
int cnt;
int val[maxn]; inline int getIdx(char a){
return a-'a';
}
void init(){
memset(ch[0],0,sizeof ch[0]);
cnt = 1;
memset(val,0,sizeof val);
}
void insert(string st){
int u = 0;
for(int i = 0; i < st.size(); i++){
int k = getIdx(st[i]);
if(!ch[u][k]){
ch[u][k] = cnt;
val[cnt]++;
cnt++;
}else{
val[ch[u][k]]++;
}
u = ch[u][k];
}
}
int query(string st){
int u = 0;
for(int i = 0; i < st.size(); i++){
int k = getIdx(st[i]);
if(!ch[u][k]) return 0;
u = ch[u][k];
}
return val[u];
}
int main(){ string st;
init();
while(getline(cin,st)&&st.size()){
insert(st);
}
while(cin >> st){
cout<<query(st)<<endl;
} return 0;
}
HDOJ1251-统计难题(trie树入门)的更多相关文章
- hdu 1251 统计难题(trie树入门)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU - 1251 统计难题(trie树)
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- hdoj1251 统计难题 字典树
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU1251 统计难题 Trie树
题目很水,但毕竟是自己第一道的Trie,所以还是发一下吧.Trie的更多的应用慢慢学,AC自动机什么的也慢慢学.... #include<iostream> #include<cst ...
- HDU1251 统计难题 trie树 简单
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 找前缀数量 裸模板 #include<cstdio> #include<cstr ...
- Trie树入门
Trie树入门 貌似很多人会认为\(Trie\)是字符串类型,但是这是数据结构!!!. 详情见度娘 下面开始进入正题. PS:本文章所有代码未经编译,有错误还请大家指出. 引入 先来看一个问题 给 ...
- hdu 1251 统计难题 trie入门
统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- hdu_1251统计难题(字典树Trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- WinHeap.H
网上找到的,对 Windows Heap 有详细的定义. // file winheap.h typedef void VOID; typedef unsigned __int8 UINT8; typ ...
- Eclipse下Maven插件配置
要做一个基于C/S架构的汽车租赁系统,由于在实习期间接触过一些Java和SpringMVC,Spring,Hibernate的东西,所以决定使用这个框架组合来完成这个项目. 首先是Maven的配置,为 ...
- Xaml 页面布局学习
对于一开始设计xaml界面的初学者,总是习惯性的拖拽控件进行布局,这样也许方便.简单.快捷,但偶尔会出现一些小错误, 当需要将控件进行很细微的挪动时也比较吃力. 这里,我个人建议用一些代码将xaml界 ...
- prototype constructor __proto__
constructor, prototype, __proto__ 详解
- OREACLE 数据库建表 添加判断表是否存在 不存在则新建
declare cnt number; begin ---查询要创建的表是否存在 select count(*)into cnt from user_tables where table_n ...
- 武汉科技大学ACM:1008: 明明的随机数
Problem Description 明明想在学校中请一些同学一起做一项问卷 调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个, ...
- CSS布局注意(纯属个人总结)
和CSS样式有关多用class,和后台数据有关多用id. 1.使用绝对定位时(偏移量如:top,left...),如果父类没有相对定位,使用绝对定位的元素是相对根元素(即<html>标签) ...
- Cocos2d-x 使用物理引擎进行碰撞检测
[转自]: http://blog.csdn.net/cbbbc/article/details/38541099 通常在游戏简单逻辑判断和模拟真实的物理世界时,我们只需要在定时器中判断游戏中各个精灵 ...
- js中给函数传参函数时,函数加括号与不加括号的区别
<!doctype html><html><head><script> function show() { alert("123") ...
- java8新特性学习
lambda语法 语法组成为三部分:参数列表.箭头符号“->”.代码块 lambda语法的比jdk1.8之前的要通过匿名类实现Runnable接口,代码上要少,而且它支持访问外部变量 strea ...