http://acm.hdu.edu.cn/showproblem.php?pid=5384

Problem Description
Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation" (ronpa).

Now, Stilwell is playing this game. There are n verbal
evidences, and Stilwell has m "bullets".
Stilwell will use these bullets to shoot every verbal evidence.

Verbal evidences will be described as some strings Ai,
and bullets are some strings Bj.
The damage to verbal evidence Ai from
the bullet Bj is f(Ai,Bj).

f(A,B)=∑i=1|A|−|B|+1[ A[i...i+|B|−1]=B ]

In other words, f(A,B) is
equal to the times that string B appears
as a substring in string A.
For example: f(ababa,ab)=2, f(ccccc,cc)=4

Stilwell wants to calculate the total damage of each verbal evidence Ai after
shooting all m bullets Bj,
in other words is ∑mj=1f(Ai,Bj).

 

Input
The first line of the input contains a single number T,
the number of test cases.
For each test case, the first line contains two integers n, m.
Next n lines,
each line contains a string Ai,
describing a verbal evidence.
Next m lines,
each line contains a string Bj,
describing a bullet.

T≤10
For each test case, n,m≤105, 1≤|Ai|,|Bj|≤104, ∑|Ai|≤105, ∑|Bj|≤105
For all test case, ∑|Ai|≤6∗105, ∑|Bj|≤6∗105, Ai and Bj consist
of only lowercase English letters

 

Output
For each test case, output n lines,
each line contains a integer describing the total damage of Ai from
all m bullets, ∑mj=1f(Ai,Bj).
 

Sample Input

1
5 6
orz
sto
kirigiri
danganronpa
ooooo
o
kyouko
dangan
ronpa
ooooo
ooooo
 

Sample Output

1
1
0
3
7
/**
hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数
*/
#include<string.h>
#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
char str[100010][10010];
int num[100010],n,m;
struct Trie
{
int next[10010*50][28],fail[10010*50],end[10010*50];
int root,L;
int newnode()
{
for(int i=0; i<26; i++)
{
next[L][i]=-1;
}
end[L++]=-1;
return L-1;
}
void init()
{
L=0;
root=newnode();
}
void insert(char *s)
{
int len=strlen(s);
int now=root;
for(int i=0; i<len; i++)
{
if(next[now][s[i]-'a']==-1)
{
next[now][s[i]-'a']=newnode();
}
now=next[now][s[i]-'a'];
}
if(end[now]==-1)///标记模式串出现的次数
{
end[now]=1;
}
else
{
end[now]++;
}
}
void build()
{
queue<int>Q;
fail[root]=root;
for(int i=0; i<26; i++)
{
if(next[root][i]==-1)
{
next[root][i]=root;
}
else
{
fail[next[root][i]]=root;
Q.push(next[root][i]);
}
}
while(!Q.empty())
{
// printf("**\n");
int now=Q.front();
Q.pop();
for(int i=0; i<26; i++)
{
if(next[now][i]==-1)
next[now][i]=next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
void query(char* s)
{
memset(num,0,sizeof(num));
int len=strlen(s);
int now=root;
for(int i=0; i<len; i++)
{
now=next[now][s[i]-'a'];
int temp=now;
while(temp!=root)
{
if(end[temp]!=-1)///统计全部模式串出现的次数,num数组在0~m之间定能取到全部end[temp]必不大于m
{
num[end[temp]]+=end[temp];
}
temp=fail[temp];
}
}
int ans=0;
for(int i=0; i<=m; i++)
{
if(num[i]>0)
ans+=num[i];
}
printf("%d\n",ans);
}
} ac;
char s[10005];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
{
scanf("%s",str[i]);
}
ac.init();
for(int i=0; i<m; i++)
{
scanf("%s",s);
ac.insert(s);
}
ac.build();
//printf("**\n");
for(int i=0; i<n; i++)
{
ac.query(str[i]);
}
}
return 0;
}

hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数的更多相关文章

  1. NYOJ 1085 数单词 (AC自己主动机模板题)

    数单词 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 为了可以顺利通过英语四六级考试,如今大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中能够通过六 ...

  2. 【HDU】病毒侵袭(AC自己主动机模板题)

    AC自己主动机的模板题.因为输入的字符串中的字符不保证全为小写字母.所以范围应该在130之前,而前31位字符是不可能出如今字符串的(不懂得查下ACSII表即可了).所以仅仅须要开的结点数组大小为130 ...

  3. HDU 2222 Keywords Search(AC自己主动机模板题)

    题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...

  4. HDU 5384 Danganronpa (AC自己主动机模板题)

    题意:给出n个文本和m个模板.求每一个文本中全部模板出现的总次数. 思路:Trie树权值记录每一个模板的个数.对于每一个文本跑一边find就可以. #include<cstdio> #in ...

  5. AC自己主动机模板

    AC自己主动机模板-- /* * AC自己主动机模板 * 用法: * 1.init() : 初始化函数 * 2.insert(str) : 插入字符串函数 * 3.build() : 构建ac自己主动 ...

  6. HDU 2222 Keywords Search AC自己主动机入门题

    单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...

  7. hdu2222--Keywords Search+AC自己主动机模板

    题目链接:pid=2222">点击进入 KMP对模式串进行处理.然后就能够方便的推断模式串是否在目标串中出现了:这显示适合一个模式串多个目标串的情况.可是假设模式串有多个,这时假设还用 ...

  8. hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. AC自己主动机模板(数组实现版)

    BY 九野 做了一道题,用我的那种写法华丽丽的超时了.,无奈学一学数组实现的 #include<stdio.h> #include<string.h> #include< ...

随机推荐

  1. JavaScript笔记(6)

    一.Date Date实例用来处理日期和时间.Date对象基于1970年1月1日(格林威治时间)世界标准时间起经过的毫秒数.常用:new Date();new Date(value);new Date ...

  2. zip---解压缩文件

    zip命令可以用来解压缩文件,或者对文件进行打包操作.zip是个使用广泛的压缩程序,文件经它压缩后会另外产生具有“.zip”扩展名的压缩文件. 语法 zip(选项)(参数) 选项 -A:调整可执行的自 ...

  3. react-native-swiper苹果正常显示,Android不显示

    在使用react-native-swiper时,最好不要放到(FlatList , SectionList,ListView,ScrollView 等)组件中,否则Android 可能不会正常显示图片 ...

  4. Mysql学习总结(9)——MySql视图原理讲解与使用大全

    一. 视图概述 视图是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的查询所引用的表,并且 ...

  5. .netcore2.1开发部署及在centos7.x下的部署

    .netcore2.1的优势毋容置疑,具体的性能建议去实际test对比,相对于之前的.netfx不知道快了多少.选择C#作为后端开发语言,主要基于以下三点: 1)代码优雅 : 2)快速搭建一套小型企业 ...

  6. Eclipse+PyDev解决中文输入和注释问题

    Eclipse的设置 window->preferences->general->editors->text editors->spelling->encoding ...

  7. xgboost参数调优的几个地方

    tree ensemble里面最重要就是防止过拟合.  min_child_weight是叶子节点中样本个数乘上二阶导数后的加和,用来控制分裂后叶子节点中的样本个数.样本个数过少,容易过拟合.  su ...

  8. BZOJ 2708 [Violet 1]木偶 DP

    题意:id=2708">链接 方法: DP 解析: 这题太神辣. 做梦都没想到DP啊,反正我不会. 先谈一个我有过的错的想法. 最小费用最大流? 能匹配的边连费用为1的,不能匹配的连费 ...

  9. jquery09--Callbacks : 回调对象

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  10. 浏览器下管理Linux系统--记webmin的使用

    本文介绍一款浏览器方式来管理linux的一种方式,这款软件就叫webmin,Webmin 让您能够在远程使用支持 HTTPS (SSL 上的 HTTP)协议的 Web 浏览器通过 Web 界面管理您的 ...