AC自己主动机裸题

Danganronpa

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

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
 

Source
 
/* ***********************************************
Author :CKboss
Created Time :2015年08月13日 星期四 13时55分22秒
File Name :1005.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long int LL; const int maxn=110100; int ch[maxn][26],fail[maxn];
LL End[maxn];
int root,sz; int newnode()
{
memset(ch[sz],-1,sizeof(ch[sz]));
End[sz++]=0;
return sz-1;
} void init()
{
sz=0; root=newnode();
} void insert(const char str[])
{
int len=strlen(str);
int now=root;
for(int i=0;i<len;i++)
{
int& temp=ch[now][str[i]-'a'];
if(temp==-1) temp=newnode();
now=temp;
}
End[now]++;
} void build()
{
queue<int> q;
fail[root]=root;
for(int i=0;i<26;i++)
{
int &temp=ch[root][i];
if(temp==-1) temp=root;
else
{
fail[temp]=root;
q.push(temp);
}
}
while(!q.empty())
{
int now=q.front(); q.pop();
for(int i=0;i<26;i++)
{
if(ch[now][i]==-1)
{
ch[now][i]=ch[fail[now]][i];
}
else
{
fail[ch[now][i]]=ch[fail[now]][i];
q.push(ch[now][i]);
}
}
}
} LL query(const char str[])
{
int len=strlen(str);
int now=root;
LL ret=0;
for(int i=0;i<len;i++)
{
now=ch[now][str[i]-'a'];
int temp=now;
while(temp!=root)
{
ret+=End[temp];
temp=fail[temp];
}
}
return ret;
} int n,m;
string Ts[maxn],Tp[maxn]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++) cin>>Ts[i]; init();
for(int i=0;i<m;i++)
{
cin>>Tp[i];
insert(Tp[i].c_str());
} build(); for(int i=0;i<n;i++)
{
LL as=query(Ts[i].c_str());
cout<<as<<endl;
}
} return 0;
}

HDOJ 5384 Danganronpa AC自己主动机的更多相关文章

  1. 【HDU 5384】Danganronpa(AC自己主动机)

    看官方题解貌似就是个自己主动机裸题 比赛的时候用kuangbin的AC自己主动机模板瞎搞的,居然A了,并且跑的还不慢.. 存下模板吧 #include<cstdio> #include&l ...

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

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

  3. AC自动机 HDOJ 5384 Danganronpa

    题目传送门 /* 题意:多个文本串,多个模式串在每个文本串出现的次数 AC自动机:这就是一道模板题,杭电有道类似的题目 */ /************************************ ...

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

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

  5. hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数

    http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...

  6. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  7. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  8. 【UVA】1449-Dominating Patterns(AC自己主动机)

    AC自己主动机的模板题.须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果. 14360841 1449 option=com_onlinejudge& ...

  9. POJ 3691 &amp; HDU 2457 DNA repair (AC自己主动机,DP)

    http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...

随机推荐

  1. Windows Server 2012 R2 with Update (x64) - DVD (Chinese-Simplified)

    http://www.msdn.hk/html/2014/1404.html Windows Server 2012 R2 with Update (x64) - DVD (Chinese-Simpl ...

  2. 深度学总结:skip-gram pytorch实现

    文章目录 skip-gram pytorch 朴素实现网络结构训练过程:使用nn.NLLLoss()batch的准备,为unsupervised,准备数据获取(center,contex)的pair: ...

  3. sqlserver差异备份3117

    1.出现错误"3117" 2.完整备份/选项/不对数据库执行任何操作 3.数据库显示"正在还原" 4.差异备份/选项/回滚 5.数据库完整备份与差异备份成功

  4. 01matplotlib

    一. 简介 1.概念 matplotlib:最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建. 2.作用 1)能将数据进行可视化,更直观的呈现 2)使 ...

  5. C++ Simple Message/Logging Class

    在 Qt的源码与Protobuf 的代码中,看到相同的简单消息(日志)输出的类实现,基本思路是使用宏定义,重载临时类对象,调用类方法或者通过析构函数自动调用输出方法,实现消息输出.这里以 Protob ...

  6. Intel CPU参数查询网站

    链接:https://ark.intel.com/#@Processors

  7. [LOJ] 分块九题 7

    区间加法,区间乘法,单点查询. 洛谷线段树2 屡清加法乘法的关系,定义答案为 a*mut+add 对于整块: 新的乘w,mut和add都要乘w 新的加w,add加w //Stay foolish,st ...

  8. .NET如何进行数据库连接

    在.net中连接sql server数据库,需引入System.Data.SqlClient命名空间: 1,最简单的连接方式: SqlConnection conn = new SqlConnecti ...

  9. 类函数调用与this指针

    1.定义多个对象是,C++编译器只分配一段空间存放公共的函数代码段,调用各个对象的函数时,都调用这个公共的代码片段. 每个对象的存储空间只是包含该对象数据成员所占的空间,函数代码存储在对象空间之外. ...

  10. Couchbase第一印象(架构特性)

    Couchbase第一印象(架构特性) 面向文档 保存的字节流总有一个 DOCUMENT ID(Object_ID) 高并发性,高灵活性,高拓展性,容错性好 面向文档的集群存储系统 每个文档用一个唯一 ...