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. Java 在本地开发环境部署多个 spring 项目

    修改Tomcat 的 server.xml 文件 路径:C:\JAVA\apache_tomcat_8.5.11\conf\server.xml : 每个web项目的端口号不同,且存储的目录相同但是文 ...

  2. Hadoop的安装与配置

    一.准备环境: 系统:centos6.5 64位 192.168.1.61   master 192.168.1.5     slave 二.在两台服务器上都要配置ssh免密码登录 在192.168. ...

  3. Python3 中类的反射

    1.针对类中方法的反射 # 反射的使用 class Dog(object): def __init__(self,name): self.name = name def eat(self): prin ...

  4. ubuntu - 14.04,该如何分区安装(初学者或不用它作为生成环境使用)?

    ubuntu14.04,实际上现在它的安装很简单了,全图形界面,可以选择母语,但是实际使用起来如果分区不当,会让我们付出惨痛的代价,那么我们应该怎么分区安装呢? 如果我们并不是把它作为专业的服务器,或 ...

  5. utf-8-BOM删除bom

    utf-8  bom,去除bom //开始 function file_bom($wenjian,$remove = true) { //读取文件,将文件写入字符串    $contents = fi ...

  6. 【bzoj1853】: [Scoi2010]幸运数字 数论-容斥原理

    [bzoj1853]: [Scoi2010]幸运数字 预处理出所有幸运数字然后容斥原理 但是幸运数字是2logn个数的 直接搞会炸 所以把成倍数的处理掉 然后发现还是会T 所以数字要从大到小处理会快很 ...

  7. 51nod1453(排列组合)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1453 题意: 中文题诶~ 思路: 因为最后一个球总是在编号比 ...

  8. Dynamic Rankings(整体二分)

    Dynamic Rankings(整体二分) 带修区间第k小.\(n,q\le 10^4\). 这次我们旧瓶装新酒,不用带修主席树.我们用整体二分!整体二分是啥东西呢? 二分答案可以解决一次询问的问题 ...

  9. loj #2023. 「AHOI / HNOI2017」抛硬币

    #2023. 「AHOI / HNOI2017」抛硬币   题目描述 小 A 和小 B 是一对好朋友,他们经常一起愉快的玩耍.最近小 B 沉迷于**师手游,天天刷本,根本无心搞学习.但是已经入坑了几个 ...

  10. phantomjs截图中文网站网页页面乱码,安装字体解决

    用phantomjs去截取中文页面的网站可能会出现乱码的情况,也就是截图中中文的位置全是方框. 解决办法就是安装字体. 在centos中执行:yum install bitmap-fonts bitm ...