hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数
http://acm.hdu.edu.cn/showproblem.php?pid=5384
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).
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
each line contains a integer describing the total damage of Ai from
all m bullets, ∑mj=1f(Ai,Bj).
5 6
orz
sto
kirigiri
danganronpa
ooooo
o
kyouko
dangan
ronpa
ooooo
ooooo
1
0
3
7
/**
hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数
*/
#include<string.h>
#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
char str[100010][10010];
int num[100010],n,m;
struct Trie
{
int next[10010*50][28],fail[10010*50],end[10010*50];
int root,L;
int newnode()
{
for(int i=0; i<26; i++)
{
next[L][i]=-1;
}
end[L++]=-1;
return L-1;
}
void init()
{
L=0;
root=newnode();
}
void insert(char *s)
{
int len=strlen(s);
int now=root;
for(int i=0; i<len; i++)
{
if(next[now][s[i]-'a']==-1)
{
next[now][s[i]-'a']=newnode();
}
now=next[now][s[i]-'a'];
}
if(end[now]==-1)///标记模式串出现的次数
{
end[now]=1;
}
else
{
end[now]++;
}
}
void build()
{
queue<int>Q;
fail[root]=root;
for(int i=0; i<26; i++)
{
if(next[root][i]==-1)
{
next[root][i]=root;
}
else
{
fail[next[root][i]]=root;
Q.push(next[root][i]);
}
}
while(!Q.empty())
{
// printf("**\n");
int now=Q.front();
Q.pop();
for(int i=0; i<26; i++)
{
if(next[now][i]==-1)
next[now][i]=next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
void query(char* s)
{
memset(num,0,sizeof(num));
int len=strlen(s);
int now=root;
for(int i=0; i<len; i++)
{
now=next[now][s[i]-'a'];
int temp=now;
while(temp!=root)
{
if(end[temp]!=-1)///统计全部模式串出现的次数,num数组在0~m之间定能取到全部end[temp]必不大于m
{
num[end[temp]]+=end[temp];
}
temp=fail[temp];
}
}
int ans=0;
for(int i=0; i<=m; i++)
{
if(num[i]>0)
ans+=num[i];
}
printf("%d\n",ans);
}
} ac;
char s[10005];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
{
scanf("%s",str[i]);
}
ac.init();
for(int i=0; i<m; i++)
{
scanf("%s",s);
ac.insert(s);
}
ac.build();
//printf("**\n");
for(int i=0; i<n; i++)
{
ac.query(str[i]);
}
}
return 0;
}
hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数的更多相关文章
- NYOJ 1085 数单词 (AC自己主动机模板题)
数单词 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 为了可以顺利通过英语四六级考试,如今大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中能够通过六 ...
- 【HDU】病毒侵袭(AC自己主动机模板题)
AC自己主动机的模板题.因为输入的字符串中的字符不保证全为小写字母.所以范围应该在130之前,而前31位字符是不可能出如今字符串的(不懂得查下ACSII表即可了).所以仅仅须要开的结点数组大小为130 ...
- HDU 2222 Keywords Search(AC自己主动机模板题)
题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...
- HDU 5384 Danganronpa (AC自己主动机模板题)
题意:给出n个文本和m个模板.求每一个文本中全部模板出现的总次数. 思路:Trie树权值记录每一个模板的个数.对于每一个文本跑一边find就可以. #include<cstdio> #in ...
- AC自己主动机模板
AC自己主动机模板-- /* * AC自己主动机模板 * 用法: * 1.init() : 初始化函数 * 2.insert(str) : 插入字符串函数 * 3.build() : 构建ac自己主动 ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
- hdu2222--Keywords Search+AC自己主动机模板
题目链接:pid=2222">点击进入 KMP对模式串进行处理.然后就能够方便的推断模式串是否在目标串中出现了:这显示适合一个模式串多个目标串的情况.可是假设模式串有多个,这时假设还用 ...
- hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- AC自己主动机模板(数组实现版)
BY 九野 做了一道题,用我的那种写法华丽丽的超时了.,无奈学一学数组实现的 #include<stdio.h> #include<string.h> #include< ...
随机推荐
- ES6学习基础
1.let和const 与var不同,新的变量声明方式带来了一些不一样的特性,其中最重要的两个特性就是提供了块级作用域与不再具备变量提升 { let a = 20; } console.log(a); ...
- 学习参考《零基础入门学习Python》电子书PDF+笔记+课后题及答案
国内编写的关于python入门的书,初学者可以看看. 参考: <零基础入门学习Python>电子书PDF+笔记+课后题及答案 Python3入门必备; 小甲鱼手把手教授Python; 包含 ...
- Android 第三方分享中遇到的问题以及解决方案
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 第三方登录和分享过程中难免遇到各种纠结的问题,下面将我遇到的分享给大家. 先说第三方登录 1.首先要 ...
- easyui datagird 总计栏
在使用easyui 的表格的时.非常多时候须要加一个总计栏,当然easyui中有加总计栏的方法,写一个footer就好,然而我认为这样的方法并不好.由于加入的总计栏是和列表每一个单元格相应的,有长度限 ...
- [python]CompressionError: bz2 module is not available
事情是这种,在centos6 上本来是python2.6 然后我下载了一个python2.7.5 安装之后,把默认python改动为python2.7.5版本号. 使用pip安装twisted的时候出 ...
- Vue移动端flexible.js+MuseUi
因为公司有个项目需求,手机端的.之前就写了一个一样的项目,只不过是用原生的写的,心想刚写了个vue后台管理系统,何不也用vue写,所有就没有把之前的利用过来.那么问题来了,要让手机端自适应我们该怎么做 ...
- python3 requests 模块 json参数和data参数区别
json 表示使用application/json方式提交请求 data 使用application/form-urlencode方式提交请求
- 00081_List接口
1.List接口介绍 (1)有序的 collection(也称为序列).此接口的用户可以对列表中每个元素的插入位置进行精确地控制.用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元 ...
- 【Codeforces Round #456 (Div. 2) C】Perun, Ult!
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] set1 < pair < int,int > > set1;记录关键点->某个人怪物永远打不死了,第 ...
- UVA 11346 Probability (几何概型, 积分)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=2321">https://uva ...