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个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...
随机推荐
- grails通过findBy或findBy查找的结果集进行排序
原文:http://grails.org/doc/2.3.x/ref/Domain%20Classes/list.html list Purpose Lists instances of the do ...
- Mac系统下安装PIL
安装PIL依赖JPEG.Freetype.LittleCMS, 首先安装这三个环境(第一至三步): 第一步:安装JPEG http://www.ijg.org/files/jpegsrc.v8c.ta ...
- python学习之旅:array 转 list
最近学习python,记录学习的点滴. >>> import numpy as np >>> a = np.array([[1,2],[3,4]]) >> ...
- C++的类为什么要用指针
这个问题原来是这样的: C++的对象和C#不同,C++的对象是放在栈区的,C#的对象是引用类型. 这就意味着,C++进行类型转换的时候,由于调用了复制构造函数,其转换后,对象会丢弃一部分信息. 派生类 ...
- 以k个元素为一组反转单链表
Example: input: 1->2->3->4->5->6->7->8->NULL and k = 3 output:3->2->1- ...
- 关于-webkit-tap-highlight-color的一些事儿
这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就会出现一个半透明的灰色背景.要重设这个表现,你可以设置-webkit-tap- ...
- Struts2 DMI的使用
Struts2的Action类中可以放置多个方法并在struts.xml中配置供页面调用.只要方法符合execute()方法的标准即返回类型是String就可以了. 同一个Action多个方法调用的方 ...
- poj 2488A Knight's Journey
#include<cstdio> #include<cstring> #define MAXN 26 using namespace std; ,,-,,-,,-,}; ,-, ...
- [Android] 输入系统(二)
在上一篇文章的最后,我们发现InputDispatcher是调用了InputChannel->sendMessage把键值发送出去,那么相应的,也有接收键值的地方.接收函数是InputChann ...
- KeilMDK4.7a下载地址/中文乱码解决/自动关联设置
推荐地址1,速度更快(确定为4.7a版本,且含注册机):1.http://www.mcuzone.com/software/keil/MDK470.RAR 2.http://kuai.xunlei.c ...