HDU 5384——Danganronpa——————【AC自动机】
Danganronpa
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 582 Accepted Submission(s): 323
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).
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).
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
#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自动机】的更多相关文章
- Hdu 5384 Danganronpa (AC自动机模板)
题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222 Keywords ...
- hdu 5384 Danganronpa
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 思路:没学自动机时以为是道KMP然后就tle了好几把,AC自动机模板题 #include<cs ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- 2017多校第6场 HDU 6096 String AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...
- 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 ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- Keywords Search - HDU 2222(AC自动机模板)
题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串. 分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...
- hdu 6096---String(AC自动机)
题目链接 Problem Description Bob has a dictionary with N words in it.Now there is a list of words in whi ...
- HDU 2296 Ring [AC自动机 DP 打印方案]
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...
随机推荐
- winform改变控件的外形
GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(0, 0, 40, 40); Region region = new Region(gp); c ...
- framwork maven的配置及使用
maven的配置及使用 一.什么是maven: 我们在开发项目的过程中,会使用一些开源框架.第三方的工具等等,这些都是以jar包的方式被项目所引用,并且有些jar包还会依赖其他的jar包,我们同样需要 ...
- UINavigationController + UIScrollView组合,视图尺寸的设置探秘(三)
还是在苹果的 View Controller Catalog for iOS 文章中找到答案.文中提到了两点: 1.If the navigation bar or toolbar are visib ...
- 形态形成场(矩阵乘法优化dp)
形态形成场(矩阵乘法优化dp) 短信中将会涉及前\(k\)种大写字母,每个大写字母都有一个对应的替换式\(Si\),替换式中只会出现大写字母和数字,比如\(A→BB,B→CC0,C→123\),代表 ...
- loj #2254. 「SNOI2017」一个简单的询问
#2254. 「SNOI2017」一个简单的询问 题目描述 给你一个长度为 NNN 的序列 aia_iai,1≤i≤N1\leq i\leq N1≤i≤N,和 qqq 组询问,每组询问读入 l1 ...
- Eclipse中使用Spring IOC容器的具体方法
1.通过IOC容器创建对象,并为属性赋值 在IOC容器本身对象创建时(xml文件加载时),会将配置文件中配置好的bean先创建出来,按照xml文件中配置的先后顺序创建 <bean id=&quo ...
- 本地命令上传文件到服务器以及linux编辑过程中非正常退出问题
一.上传文件到linux服务器首先从你本地切换到你要上传文件的目录,接下来:scp 文件名字 服务器用户名字@服务器ip:存储路径例子:scp index.html root@106.75.229 ...
- Jmeter函数引用和函数重定向【转】
在jmeter中的[选项]中选择[函数助手对话框]---这些函数可以高速有效的帮助我们开展自动化编写与校验!!!!!! 如图: 重点!!!本章的侧重点不讲函数的具体使用,函数具体的使用与java类似, ...
- [Node.jS]shelljs
shelljs : https://www.npmjs.org/package/shelljs 要给可以替代Unix下shell脚本的库. require('shelljs/global'); if ...
- TensorFlow入门测试程序
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist=input_data. ...