Repository

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5048    Accepted Submission(s): 1739

Problem Description
When
you go shopping, you can search in repository for avalible merchandises
by the computers and internet. First you give the search system a name
about something, then the system responds with the results. Now you are
given a lot merchandise names in repository and some queries, and
required to simulate the process.
Input
There
is only one case. First there is an integer P
(1<=P<=10000)representing the number of the merchanidse names in
the repository. The next P lines each contain a string (it's length
isn't beyond 20,and all the letters are lowercase).Then there is an
integer Q(1<=Q<=100000) representing the number of the queries.
The next Q lines each contains a string(the same limitation as foregoing
descriptions) as the searching condition.
Output
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
Sample Input
20
ae
af
ag
ah
ai
aj
ak
al
ads
add
ade
adf
adg
adh
adi
adj
adk
adl
aes
5
b
a
d
ad
s
Sample Output
0
20
11
11
2
我好气呀 g++无限爆内存 换c++过了 无奈下顺便写了个静态的
题意就是有n个字符串,m个询问  问字符串在n个字符串中出现过多少次
比如 abcd 中有a,b,c,d,abcd,bcd,cd,bc,abc...
我们可以将abcd拆成abcd bcd cd d 分别建树,每个字符计算下数字
但abab会导致重复,所以我们设置一下标记,看代码
动态建树,g++爆内存  c++交
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<queue>
  7. #include<map>
  8. #include<set>
  9. #include<vector>
  10. #include<cstdlib>
  11. #include<string>
  12. #define eps 0.000000001
  13. typedef long long ll;
  14. typedef unsigned long long LL;
  15. using namespace std;
  16. struct tire{
  17. int id,num;
  18. tire *next[];
  19. };
  20. tire *root;
  21. void insert(char *s,int k){
  22. tire *p,*q;
  23. p=root;
  24. int len=strlen(s);
  25. for(int i=;i<len;i++){
  26. //cout<<3<<endl;
  27. int t=s[i]-'a';
  28. if(p->next[t]==NULL){
  29. q=(tire *)malloc(sizeof(tire));
  30. for(int j=;j<;j++)q->next[j]=NULL;
  31. q->num=;
  32. q->id=-;
  33. p->next[t]=q;
  34. }
  35. p=p->next[t];
  36. if(p->id!=k){
  37. p->id=k;
  38. p->num++;
  39. }
  40. }
  41. }
  42. int find(char *s){
  43. tire *p=root;
  44. int len=strlen(s);
  45. for(int i=;i<len;i++){
  46. int t=s[i]-'a';
  47. if(p->next[t]==NULL)return ;
  48. else
  49. p=p->next[t];
  50. }
  51. return p->num;
  52. }
  53. int main(){
  54. int m,n;
  55. char str[];
  56. root=(tire *)malloc(sizeof(tire));
  57. for(int i=;i<;i++)root->next[i]=NULL;
  58. root->id=-;
  59. root->num=;
  60. scanf("%d",&n);
  61. for(int i=;i<n;i++){
  62. scanf("%s",str);
  63. int len=strlen(str);
  64. for(int j=;j<len;j++){
  65. //cout<<1<<endl;
  66. insert(str+j,i);//cout<<2<<endl;
  67. }
  68. }
  69. scanf("%d",&m);
  70. while(m--){
  71. scanf("%s",str);
  72. cout<<find(str)<<endl;
  73. }
  74. }

接下来是一个静态的字典树(节省内存) c++ g++都可以过

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<queue>
  7. #include<map>
  8. #include<set>
  9. #include<vector>
  10. #include<cstdlib>
  11. #include<string>
  12. #define maxnode 500000
  13. #define sigma_size 30
  14. #define eps 0.000000001
  15. typedef long long ll;
  16. typedef unsigned long long LL;
  17. using namespace std;
  18. int ch[maxnode][sigma_size];
  19. int val[maxnode];
  20. int flag[maxnode];
  21. int sz;
  22. void init(){
  23. memset(ch[],,sizeof(ch[]));
  24. sz=;
  25. }
  26. int idx(char c){
  27. return c-'a';
  28. }
  29. void insert(char *s,int k){
  30. int u=;
  31. int len=strlen(s);
  32. for(int i=;i<len;i++){
  33. int c=idx(s[i]);
  34. if(ch[u][c]==){
  35. memset(ch[sz],,sizeof(ch[sz]));
  36. val[sz]=;
  37. ch[u][c]=sz++;
  38. }
  39. u=ch[u][c];
  40. if(flag[u]!=k){
  41. val[u]++;
  42. flag[u]=k;
  43. }
  44. }
  45. }
  46. int find(char *s){
  47. int u=;
  48. int len=strlen(s);
  49. for(int i=;i<len;i++){
  50. int c=idx(s[i]);
  51. if(ch[u][c]==)return ;
  52. u=ch[u][c];
  53. }
  54. return val[u];
  55. }
  56. int main(){
  57. int m,n;
  58. init();
  59. memset(flag,-,sizeof(flag));
  60. //for(int i=0;i<10;i++)cout<<flag[i]<<" ";
  61. char str[];
  62. scanf("%d",&n);
  63. for(int i=;i<n;i++){
  64. scanf("%s",str);
  65. int len=strlen(str);
  66. for(int j=;j<len;j++){
  67. //cout<<1<<endl;
  68. insert(str+j,i);//cout<<2<<endl;
  69. }
  70. }
  71. scanf("%d",&m);
  72. while(m--){
  73. scanf("%s",str);
  74. cout<<find(str)<<endl;
  75. }
  76. }
Source

hdu 2846(字典树)的更多相关文章

  1. Repository HDU - 2846 字典树

    题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...

  2. hdu 2846 字典树变形

    mark: 题目有字串匹配的过程 有两点 1.为了高效的匹配子串 可以把所有的子串都预处理进去 然后字典树计数就放在最后面 2.在同一个母串处理自串的时候 会有重复的时候 比如abab  这里去重用个 ...

  3. HDU 5687 字典树插入查找删除

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...

  4. HDU 5384 字典树、AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...

  5. hdu 2112(字典树+最短路)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. hdu 2072(字典树模板,set,map均可做)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...

  7. Chip Factory HDU - 5536 字典树(删除节点|增加节点)

    题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...

  8. hdu 1251 字典树的应用

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

  9. hdu 2896 字典树解法

    #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> ...

  10. Phone List HDU - 1671 字典树

    题意:给出一堆一组一组的数字  判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断  不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...

随机推荐

  1. redis集群——RPLR简笔(Redis+PostgreSQL+Linux(centos7)+RabbitMQ)

    使用的是centos7. 1.下载最新redis源码,解压(2016-05-12最新版本为3.2.0,3.0及以上才有官方集群) 2.进入源码根目录(此目录下的redis-stable目录),找到ut ...

  2. POJ_3013_最短路

    Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23630   Accepted: 5 ...

  3. sql日期提取

    --插入数据修改不行:必须提供学号 insert into Student(生日类型) values('阳历') --把月份提取出来 显示两位数 select DATENAME(month,getda ...

  4. Java单元测试 - TestNG

    官网 Eclipse安装TestNG插件 与Junit相比 从Junit发展而来,开发者就是Junit小组的一个人 Test Suite不再需要硬编码,就像cf自动登录的脚本中一样,可以写到一个xml ...

  5. pandas操作,按序号取列,按条件筛选,df格式转换等

    这几天遇到比较多的dataframe操作,频繁使用,在此整理记录下,方便查找. 1.num为列的数字序号,name=df.columns[num],返回的是column的字符串名字,df[name]= ...

  6. linux 的sed命令解释 sed ':t;N;s/\n/,/;b t' 将换行符换成逗号

    linux 的sed命令解释 sed ':t;N;s/\n/,/;b t' 将换行符换成逗号 实现的功能是吧换行符换成逗号了,自己试验过. 求解释,:t N b t 都是什么意思??? :t 定义la ...

  7. uva1584 Circular Sequence(Uva-1584)

    vj:https://vjudge.net/problem/UVA-1584 这个题讲的是一个圆环,圆环上面有一堆字母,找出字典序最小的那一圈 这个题我觉得直接用c语言的strcmp那一套感觉真是用不 ...

  8. 在Python脚本中调用Django环境(方便、右键运行,可用于ORM测试)

    随便创建一个py文件即可: import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTINGS_MODUL ...

  9. 使用Python PIL库中的Image.thumbnail函数裁剪图片

    今天,是我来到博客园的第五天,发现自己还没有头像,想着上传ubuntu系统中我很喜欢的一个背景图片来当头像,但是因为图片过大,上传失败了.那么,我们如何使用python中强大的PIL库来进行图片裁剪呢 ...

  10. 8 pandas模块,多层索引

      1 创建多层索引     1)隐式构造         最常见的方法是给DataFrame构造函数的index参数传递两个或更多的数组           · Series也可以创建多层索引    ...