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个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...
随机推荐
- inline-block(行内区块元素)的详解和应用
说inline-block(行内区块元素)之前,先说下他另外的2个兄弟 display:inline; 内联元素,简单来说就是在同一行显示.他没有高度,给内联元素设置width和height是没效果的 ...
- 多项式求和 AC 杭电
多项式求和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- Linux 下 将使用Python-Django开发的web应用布置到服务器上(亲测有效)
写在前面: Django是一个卓越的新一代Web框架,相信使用Python的人对此并不陌生,但将我们完成的web应用布置到到服务器上并不是一件容易的事情. Django详细的教程可以参考http:// ...
- Linux 下安装Python框架django建立与mysql的连接
0.基本环境说明: a. Ubuntu 14.04 64bit b. python 2.7.6 c. django 1.8 d. django-registration e. django-widge ...
- raspberry pi 3 截图及查看
RASPBIAN JESSIE WITH PIXEL Image with PIXEL desktop based on Debian Jessie Version:November 2016 Rel ...
- noi 3531 判断整除
3531:判断整除 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 一个给定的正整数序列,在每个数之前都插入+号或-号后计算它们的和.比如序列:1.2.4共有 ...
- Keil C51库函数原型列表
//1. CTYPE.H bit isalnum(char c): bit isalpha(char c): bit iscntrl(char c): bit isdigit(char c): bit ...
- 如何让窗口控件半透明(控件在Paint自己时,首先向主窗口询问,获取主窗口上控件所在区域的背景图)
在网上关于窗口视觉效果,有2个问题被问得最多:第一个是如何让窗口边框有阴影效果?第二个是如何让窗口控件有半透明效果? 对于第一个问题,我们的答案是用双层窗口模拟或是用Layered Window.在X ...
- 【HDOJ】1726 God’s cutter
简单DP. /* 1726 */ #include <cstdio> #include <cstring> #include <cstdlib> #define M ...
- Table Lookup
做OJ的时候,做过类似的,即hash.算法很简单,关键是书上写的和做OJ,是完全不同的风格.有很多值得学习的地方. /* * Table Lookup * 详见<<C程序设计语言>& ...