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++交
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
struct tire{
int id,num;
tire *next[];
};
tire *root;
void insert(char *s,int k){
tire *p,*q;
p=root;
int len=strlen(s);
for(int i=;i<len;i++){
//cout<<3<<endl;
int t=s[i]-'a';
if(p->next[t]==NULL){
q=(tire *)malloc(sizeof(tire));
for(int j=;j<;j++)q->next[j]=NULL;
q->num=;
q->id=-;
p->next[t]=q;
}
p=p->next[t];
if(p->id!=k){
p->id=k;
p->num++;
}
}
}
int find(char *s){
tire *p=root;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-'a';
if(p->next[t]==NULL)return ;
else
p=p->next[t];
}
return p->num;
}
int main(){
int m,n;
char str[];
root=(tire *)malloc(sizeof(tire));
for(int i=;i<;i++)root->next[i]=NULL;
root->id=-;
root->num=;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",str);
int len=strlen(str);
for(int j=;j<len;j++){
//cout<<1<<endl;
insert(str+j,i);//cout<<2<<endl;
}
}
scanf("%d",&m);
while(m--){
scanf("%s",str);
cout<<find(str)<<endl;
}
}

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

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define maxnode 500000
#define sigma_size 30
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
int ch[maxnode][sigma_size];
int val[maxnode];
int flag[maxnode];
int sz;
void init(){
memset(ch[],,sizeof(ch[]));
sz=;
}
int idx(char c){
return c-'a';
}
void insert(char *s,int k){
int u=;
int len=strlen(s);
for(int i=;i<len;i++){
int c=idx(s[i]);
if(ch[u][c]==){
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]=sz++;
}
u=ch[u][c];
if(flag[u]!=k){
val[u]++;
flag[u]=k;
}
}
}
int find(char *s){
int u=;
int len=strlen(s);
for(int i=;i<len;i++){
int c=idx(s[i]);
if(ch[u][c]==)return ;
u=ch[u][c];
}
return val[u];
}
int main(){
int m,n;
init();
memset(flag,-,sizeof(flag));
//for(int i=0;i<10;i++)cout<<flag[i]<<" ";
char str[];
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",str);
int len=strlen(str);
for(int j=;j<len;j++){
//cout<<1<<endl;
insert(str+j,i);//cout<<2<<endl;
}
}
scanf("%d",&m);
while(m--){
scanf("%s",str);
cout<<find(str)<<endl;
}
}
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. JavaScript:颜色辨别

    <script> //参考文章:http://www.cnblogs.com/xuechenlei/p/5940729.html //游戏页面:http://www.webhek.com/ ...

  2. vi 命令学习(一)

    行内移动] w word 向后移动一个单词 b back ·向前移动一个单词 行首 ^ 行首,第一个不是空白字符的位置 $ 行尾 [行数移动] gg go 文件顶部 G go 文件末尾 数字gg go ...

  3. dotfuscator 如何设置

  4. 怎么用最短时间高效而踏实地学习Linux?

    在技术行业里,人才的唯一衡量标准就是技术能力,而技术能力,就代表着你的薪资.职位.话语权.很多人都经历过,跟自己同时入行甚至入行还晚的人,成长速度却远超自己,短短两三年就拉开了差距. 秘密就在于,有些 ...

  5. PyCharm社区版+Django搭建web开发环境

    PyCharm开源社区版不像商业版那样可以直接通过Django来创建项目,必须通过以下几个步骤进行: 1. 创建项目:在cmd命令行下输入:django-admin startproject Demo ...

  6. koji

    fedora koji https://koji.fedoraproject.org/koji/ centos cbs.centos.org/koji/

  7. C++项目作业 学生管理系统

    /*Student.h*/#pragma once #include<string.h> using namespace std; #include<string> class ...

  8. rsync全网备份

    rsync备份企业方案 企业有Linux服务器又有windows服务器,备份用rsync(服务端),Linux(客户端),Windows(客户端,cwrsync,旧版本有免费版)打包压缩数据往服务器上 ...

  9. 64.root object的理解

    一.root object的理解     就是某个type对应的mapping json,包括properties,metadata(_id,_source,_type),settings(analy ...

  10. ansible - playbook(剧组)

    目录 ansible - playbook(剧组) 常用命令 五种传参方式 常用元素详解 tags handlers template when 循环 嵌套循环 ansible - playbook( ...