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. this.$refs.tabs.activeKey ref就是vue里面的id

    this.$refs.tabs.activeKey ref就是vue里面的id

  2. JS将时间戳转换为刚刚、N分钟前、今天几点几分、昨天几点几分等表示法

    使用Javascript语言,将时间戳转换为类似新浪微博的时间的表示方法. 要求转换规则: 1分钟以内显示为:刚刚 1小时以内显示为:N分钟前 当天以内显示为:今天 N点N分(如:今天 22:33) ...

  3. 基础数据类型(set集合)

    认识集合 由一个或多个确定的元素所构成的整体叫做集合. 集合中的元素有三个特征: 1.确定性(集合中的元素必须是确定的) 2.互异性(集合中的元素互不相同.例如:集合A={1,a},则a不能等于1) ...

  4. css--css选择器,伪类

    前戏 前面我们说过CSS规则由选择器和声明组成,我们要给标签设置属性,那我们就要找到对应的标签,CSS选择器可以帮我们找到我们需要的标签 css选择器有: 标签选择器 类选择器 ID选择器 全局选择器 ...

  5. C语言中函数参数传递的本质是值传递

    数组名做函数参数进行传递时,实际上是是一份该指针的拷贝. 给形参赋予其他值,并不影响实参的值. 类似于: int *p = a;    //a为数组名 p = b;          //b为数组名 ...

  6. Python 函数的初识

    1.函数的初识 函数的作用:以功能为导向 减少代码重复 # 函数试编程 # 函数以功能(完成一件事)为导向,登录 注册, # 一个函数就是一个功能,一个函数只能写一个功能 # 何时需要 何时调用,随调 ...

  7. Spring Boot 集成Angular程序

    假设 1.你已经完成了Spring Boot的示例,在浏览其中输入http://localhost:8080/index,能够返回html页面. 2.你已经完成了Angular程序,名字为quicks ...

  8. [jzoj5073 GDOI2017第二轮模拟] 影魔

    Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵 ...

  9. 算法导论 第八章 线性时间排序(python)

    比较排序:各元素的次序依赖于它们之间的比较{插入排序O(n**2) 归并排序O(nlgn) 堆排序O(nlgn)快速排序O(n**2)平均O(nlgn)} 本章主要介绍几个线性时间排序:(运算排序非比 ...

  10. luogu1502 窗口的星星

    扫描线应该打懒标记的-- #include <algorithm> #include <iostream> #include <cstdio> using name ...