HDU 1251 统计难题(字典树)
来源:互联网 发布:godaddy域名dns修改 编辑:程序博客网 时间:2024/06/18 05:56
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 43074 Accepted Submission(s): 15465
Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
bananabandbeeabsoluteacmbabbandabc
Sample Output
2310
Author
Ignatius.L
字典树裸题,数组实现。
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;#define LL long longconst int N = 400000;int tree[N][27];int val[N];int sz=1;void init(){ memset(tree,0,sizeof tree); memset(val,0,sizeof val); sz=1;}void build(char s[]){ int l=strlen(s); int u=0; for(int i=0;i<l;i++) { int j=s[i]-'a'; if(tree[u][j]==0) { tree[u][j]=sz++; } u=tree[u][j]; val[u]++; }}int query(char s[]){ int l=strlen(s); int u=0; for(int i=0;i<l;i++) { int j=s[i]-'a'; if(tree[u][j]==0) return 0; else { u=tree[u][j]; } } return val[u];}int main(){ init(); char s[13]; while(gets(s)) { if(strlen(s)==0) break; build(s); } char s1[13]; while(gets(s1)) { if(strlen(s1)==0) break; printf("%d\n",query(s1)); }}
阅读全文
0 0
- HDU 1251统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题 (字典树)
- hdu 1251 统计难题(字典树)
- HDU 1251 统计难题(字典树)
- HDU 1251 统计难题(字典树)
- HDU 1251 统计难题(字典树)
- HDU-#1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- HDU - 1251 - 统计难题(字典树)
- HDU 1251 统计难题(字典树)
- HDU 1251统计难题 (字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- HDU 1251 统计难题(字典树)
- HDU-1251 统计难题(字典树)
- Unique Binary Search Trees问题及解法
- 年-月-日
- java后端WebSocket的Tomcat实现
- bzoj 2440: [中山市选2011]完全平方数(二分+莫比乌斯函数)
- Centos7和Python安装Ice
- HDU 1251 统计难题(字典树)
- 前端学习笔记(JQuery+JavaScript)
- 为什么final类型map和list内容可以修改
- java基础7
- int argc 、char *argv[] 与 char **argv
- mysql5.7修改用户的密码和给用户授权
- MySQL存储写入性能严重抖动分析
- mxnet-ssh环境配置
- thinkphp3.2 Cli定时任务