Danganronpa

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 582    Accepted Submission(s): 323

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
 
 
题目大意:t组测试数据,下面有n个文本串,m个模式串,问每个文本串能匹配多少个模式串。
 
解题思路:AC自动机,记录模式串单词出现的次数cnt,然后直接套模板就可以了。
 
 
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+200;
char Text[maxn][maxn/10],Pattern[maxn];
struct ACnode{
ACnode *fail;
ACnode *next[26];
int cnt;
ACnode (){
fail=NULL;
for(int i=0;i<26;i++)
next[i]=NULL;
}
}*Q[maxn*10];
ACnode *newacnode(){
ACnode *tmp;
tmp=new ACnode;
tmp->cnt=0;
}
void Insert(ACnode *rt,char *s){
int len=strlen(s);
int idx;
for(int i=0;i<len;i++){
idx=s[i]-'a';
if(rt->next[idx]==NULL){
rt->next[idx]=newacnode();
}
rt=rt->next[idx];
}
rt->cnt++;
}
void BuildAC(ACnode *root){
int head,tail;
head=tail=0;
for(int i=0;i<26;i++){
if(root->next[i]!=NULL){
root->next[i]->fail=root;
Q[tail++]=root->next[i];
}
}
ACnode *p,*tmp;
while(head!=tail){
p=Q[head++];
tmp=NULL;
for(int i=0;i<26;i++){
if(p->next[i]!=NULL){
tmp=p->fail;
while(tmp!=NULL){
if(tmp->next[i]!=NULL){
p->next[i]->fail=tmp->next[i];
break;
}
tmp=tmp->fail;
}
if(tmp==NULL){
p->next[i]->fail=root;
}
Q[tail++]=p->next[i];
}
}
}
}
int Query(ACnode *root,char *str){
ACnode *p=root,*tmp=NULL;
int idx,len,ret;
ret=0;
len=strlen(str);
for(int i=0;i<len;i++){
idx=str[i]-'a';
while( p->next[idx]==NULL && p!=root )
p=p->fail;
p=p->next[idx];
if(p == NULL)
p=root;
tmp=p;
while(tmp!=root ){
ret+=tmp->cnt;
tmp=tmp->fail;
}
}
return ret;
} int main(){
int n,m,t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
ACnode *root;
root=newacnode();
for(int i=0;i<n;i++){
scanf("%s",Text[i]);
}
for(int i=0;i<m;i++){
scanf("%s",Pattern);
Insert(root,Pattern);
}
BuildAC(root);
for(int i=0;i<n;i++){
int ans=Query(root,Text[i]);
printf("%d\n",ans);
}
}
return 0;
}

  

HDU 5384——Danganronpa——————【AC自动机】的更多相关文章

  1. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

  2. hdu 5384 Danganronpa

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 思路:没学自动机时以为是道KMP然后就tle了好几把,AC自动机模板题 #include<cs ...

  3. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  4. 2017多校第6场 HDU 6096 String AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

  5. 2015 Multi-University Training Contest 8 hdu 5384 Danganronpa

    Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  6. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  7. Keywords Search - HDU 2222(AC自动机模板)

    题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串.   分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...

  8. hdu 6096---String(AC自动机)

    题目链接 Problem Description Bob has a dictionary with N words in it.Now there is a list of words in whi ...

  9. HDU 2296 Ring [AC自动机 DP 打印方案]

    Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

随机推荐

  1. 【京东助手】滑稽东试用助手 V1.6.0

    很久没写博客了,发个最近在做的一个软件! 滑稽东试用助手现有功能1.自动申请试用2.自动领取京豆3.自动签到领取京豆说明:该软件使用C#开发,若要运行此应用程序,您必须首先安装 .NET4.01.双击 ...

  2. 【1】循序渐进学 Zabbix :初识与基础依赖环境搭建( LNMP )

    写在前面的话 运维监控是一个很大的话题,在这一块个人接触的比较突出的服务主要有 Nagio 和 Zabbix 两款.而这几年跳过的公司中,Zabbix 一直都是首选且唯一选择,Nagios 没遇到. ...

  3. Mybatis基于代理Dao实现CRUD操作 及 Mybatis的参数深入

    Mybatis基于代理Dao实现CRUD操作 使用要求: 1.持久层接口和持久层接口的映射配置必须在相同的包下 2.持久层映射配置中mapper标签的namespace属性取值必须是持久层接口的全限定 ...

  4. mysql死锁问题解决

    1. 定位问题 http://blog.csdn.net/beiigang/article/details/43228361 2. 解决死锁 http://www.blogbus.com/ri0day ...

  5. AngularJS(三)——指令实战及自定义指令

    前言 上篇介绍了一些指令的应用,本篇介绍一些常用的用法格式. 内容 指令实战 下面通过输入一个名字实现实时更新文本内容. 需要的指令有: ng-app.ng-model.ng-bind.n-init ...

  6. 【bzoj2818】: Gcd 数论-欧拉函数

    [bzoj2818]: Gcd 考虑素数p<=n gcd(xp,yp)=p 当 gcd(x,y)=1 xp,yp<=n满足条件 p对答案的贡献: 预处理前缀和就好了 /* http://w ...

  7. Ceph配置文件查看修改方式

    1.     修改ceph配置文件的方式有三种:(其中包含临时和永久生效) 1)      修改所有或者指定的进程 2)      修改当前服务器进程 3)      修改配置文件 Note:在线修改 ...

  8. 《图解HTTP》阅读笔记--第四章--HTTP状态码

    第四章.返回结果的HTTP状态码前言:状态码的职责是告诉用户服务器端描述返回的请求,以便用户判断服务器处理是否正常. 状态码由三位数字和原因短语组成,其中三位数字的首位指定了响应类别:---1xx 接 ...

  9. P2381 圆圆舞蹈

    题意:一个圆,上面有n头牛(卧槽) 给出相邻两头牛顺时针的距离 问两只最远的牛的距离(min(顺时针距离,逆时针距离)) 最远距离一定$\le$距离和/2 先求个前缀和 那么问题转化为:找到 $s_j ...

  10. 使用box-shadow 实现水波、音波的效果

    用到的工具 animation box-shadow html: <div class="watersource"> </div> css: .waters ...