HUD 2846 Repository
/*
开始想耍小聪明 直接map搞
代码短 好理解 空间够
恩 很好 就是 map慢死了
T了
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
int n,m,ans;
string s,si;
map<string,int>t;
int main()
{
cin>>n;
while(n--)
{
map<string,int>f;
cin>>s;int l=s.length();
for(int i=;i<l;i++)
for(int j=i;j<l;j++)
{
si.clear();
for(int k=i;k<=j;k++)si=si+s[k];
if(!f[si])t[si]++,f[si]=;
}
}
cin>>m;
while(m--)
{
cin>>s;
cout<<t[s]<<endl;
}
return ;
}
/*
还是老老实实写字典树
这里不需要保证是前缀 只要有就可以
所以对于一个单词 我们把他所有的后缀加到字典树里
注意特殊的 abab这样的 如果按上面说的 会加两遍 ab
所以每个节点我们再维护一个last 表示这个小单词上次是在那个单词里出现
这样就ok了
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#define maxn 1000010
using namespace std;
int n,m,topt;
char s[];
struct node
{
int next[],sum,last;
}t[maxn];
void Add_tree(int st,int p)
{
int now=,l=strlen(s);
for(int i=st;i<l;i++)
{
int x=s[i]-'a';
if(t[now].next[x])now=t[now].next[x];
else
{
topt++;t[now].next[x]=topt;now=topt;
}
if(t[now].last!=p)t[now].sum++;
t[now].last=p;
}
}
int find_tree()
{
int now=,p=,l=strlen(s);
while(p<=l-)
{
int x=s[p]-'a';
if(t[now].next[x])
{
p++;now=t[now].next[x];
}
else return ;
}
return t[now].sum;
}
int main()
{
scanf("%d",&n);
for(int k=;k<=n;k++)
{
scanf("%s",s);int l=strlen(s);
for(int i=;i<l;i++)
Add_tree(i,k);
}
scanf("%d",&m);
for(int k=;k<=m;k++)
{
scanf("%s",s);
printf("%d\n",find_tree());
}
return ;
}
HUD 2846 Repository的更多相关文章
- hdu 2846 Repository
http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 2846 Repository (字典树 后缀建树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 2846 Repository(字典树,每个子串建树,*s的使用)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- poj 2846 Repository
题目大意:给你n个字符串,然后给你m个子串,看这个子串在上面的多少个串中,出现过: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846 本题可以在字 ...
- hdu 2846 Repository (字典树)
RepositoryTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 2846 Repository(字典树,标记)
题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...
- HDU 2846 Repository(字典树)
字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个id,判断它原先是属于哪个串的.有人说是AC自动机的简化,但是AC自动机我还没有做过. #include<io ...
- HDU:2846-Repository
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 2846:Repository(Trie)
http://acm.hdu.edu.cn/showproblem.php?pid=2846 题意:给出N个模式串,再给出M个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...
随机推荐
- linux文件系统结构和权限
linux文件系统的目录结构 熟话说的好,好记性不如烂笔头,虽然没用笔,但动动手指还是可以的.下面的目录结构都是摘抄过来的,动动手指来加深下印象吧,还能练习下打字速度,哈哈,多好啊. ...突然又改变 ...
- jQuery MVC 科室异步联动
//科室改变,级联医生 js $("#DepartmentId").change(function () { if (isNaN($(this).val())) { $(" ...
- Uva 120 - Stacks of Flapjacks(构造法)
UVA - 120 Stacks of Flapjacks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &a ...
- Instruments 使用指南
Instruments 用户指南 http://cdn.cocimg.com/bbs/attachment/Fid_6/6_24457_90eabb4ed5b3863.pdf 原著:Apple Inc ...
- IOS--UIDatePicker 时间选择器 的使用方法详细
IOS--UIDatePicker 时间选择器 的使用方法详细 // 主要有下面四种类型: // 日期显示. // 日期和时间显示. // 时间显示. // 倒计时选择 // UIDa ...
- android 休眠唤醒机制分析(二) — early_suspend
本文转自:http://blog.csdn.net/g_salamander/article/details/7982170 early_suspend是Android休眠流程的第一阶段即浅度休眠,不 ...
- Javascript闭包与作用域
摘自开源中国 闭包和作用域是js中比较重要的知识,自己理解起来也有一定的难度 1.Javascript的作用域是函数作用域而非块级作用域 ? 1 2 3 4 5 6 7 8 9 10 11 12 // ...
- 编写C# Windows服务,用于杀死Zsd.exe进程
最近经常在我的xp系统进程中出现Zsd.exe进程.刚开始他占用内存不是很大.但是过了一段时间就会变成几百M 机器就会变得很卡,网上说Zsd可能是病毒.所以我就想要不写一个Windows服务,让他每隔 ...
- C++类型转换总结
C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:TYPE b = (TYPE)a.C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. const_cast, ...
- TMethod的学习与使用
http://bbs.2ccc.com/topic.asp?topicid=496893