题意:给你n个母串。m个匹配串,让你求出对于每一个母串 全部匹配串出现的次数和。

思路:完全然全邝斌的模板啊。。。 凝视掉一行代码就能a。。。

代码:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define sss(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a) memset(a,0,sizeof(a))
#define ss(a,b) scanf("%d%d",&a,&b)
#define s(a) scanf("%d",&a)
#define p(a) printf("%d\n", a)
#define INF 0x3f3f3f3f
#define w(a) while(a)
#define PI acos(-1.0)
#define LL long long
#define eps 10E-9
#define N 100010
#define mod 258280327
const int SIGMA_SIZE=26;
const int MAXN=100010;
const int MAXNODE=600010;
using namespace std;
void mys(int& res)
{
int flag=0;
char ch;
while(!(((ch=getchar())>='0'&&ch<='9')||ch=='-'))
if(ch==EOF) res=INF;
if(ch=='-') flag=1;
else if(ch>='0'&&ch<='9') res=ch-'0';
while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0';
res=flag?-res:res;
}
void myp(int a)
{
if(a>9)
myp(a/10);
putchar(a%10+'0');
}
/*************************THE END OF TEMPLATE************************/
char str1[N][10001];
char s[N];
int m, ans;
struct ac_trie{
int next[500010][26], ed[500010], fail[500010];
int root, L;
int newnode(){
for(int i=0; i<26; i++) next[L][i] = -1;
ed[L++] = 0;
return L-1;
}
void init(){
L = 0;
root = newnode();
}
void m_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'];
}
ed[now]++;
}
void getfail(){
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]);
}
w(!Q.empty()){
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]);
}
}
}
}
int query(char *s){
int len = strlen(s);
int now = root;
int res = 0;
for(int i=0; i<len; i++){
now = next[now][s[i]-'a'];
int temp = now;
w(temp!=root){
res += ed[temp];
//ed[temp] = 0;
temp = fail[temp];
}
}
return res;
}
};
ac_trie ac;
int main(){
int t, n;
s(t);
w(t--){
ss(n, m);
ac.init();
for(int i=0; i<n; i++) scanf("%s",str1[i]);
for(int i=0; i<m; i++){
scanf("%s", s);
ac.m_insert(s);
}
ac.getfail();
for(int i=0; i<n; i++) p(ac.query(str1[i]));
}
return 0;
}

hdu5384的更多相关文章

  1. [2015hdu多校联赛补题]hdu5384 Danganronpa

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:函数f(A, B)定义:A.B为字符串,f(A, B)为A中有多少个不同的B(ex:f(& ...

  2. hdu5384(trie树)

    hdu5384 给定n个字符串Ai 给定m个字符串Bi 问所有的Bi在每个Ai中出现了多少次 很显然,对Bi建Trie图,然后每次用Ai去匹配的时候,不断查找当前匹配串的最长后缀,这样就能找到答案了 ...

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

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

  4. 2015 多校联赛 ——HDU5384(AC自动机)

    Sample Input 1 5 6 orz sto kirigiri danganronpa ooooo o kyouko dangan ronpa ooooo ooooo   Sample Out ...

  5. HDU-5384

    Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  6. AC自动机(模板+例题)

    首先要明白AC自动机是干什么的: AC自动机其实就是一种多模匹配算法,那么你可能会问什么叫做多模匹配算法.下面是我对多模匹配的理解,与多模与之对于的是单模,单模就是给你一个单词,然后给你一个字符串,问 ...

随机推荐

  1. loj2014 「SCOI2016」萌萌哒

    神tm st表+并查集 #include <iostream> #include <cstdio> #include <cmath> using namespace ...

  2. BugBash活动分享

    此文已由作者夏君授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. BugBash源至微软概念,翻译为<缺陷大扫除>,顾名思义是集中大家力量全面清扫Bug,确保产品质 ...

  3. 在. net平台下,推送IOS,Android,Windows Phone消息

    1,新建项目,在项目中添加引用,dll文件已经上传在百度网盘,点击下载 2,引入命名空间 using PushSharp; using PushSharp.Android; using PushSha ...

  4. Leetcode3--->无重复字符的最长子串长度

    题目:给定一个字符串string,找出string中无重复字符的最长子串. 举例: Given "abcabcbb", the answer is "abc", ...

  5. GBDT 与 XGBoost

    GBDT & XGBoost ### 回归树 单棵回归树可以表示成如下的数学形式 \[ f(x) = \sum_j^Tw_j\mathbf{I}(x\in R_j) \] 其中\(T\)为叶节 ...

  6. NKOJ1236 a^b (数论定理的应用)

              a^b 对于任意两个正整数a,b(0<=a,b<10000)计算a^b各位数字的和的各位数字的和的各位数字的和的各位数字的和. Input 输入有多组数据,每组只有一行 ...

  7. php hash防止表单

    <?php /** * Created by PhpStorm. * User: brady * Desc: * Date: 2017/7/12 * Time: 15:01 */class te ...

  8. hibernate框架的搭建与简单实现增删改

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自 ...

  9. Linux Shell系列教程之(一)Shell简介

    本文是Linux Shell系列教程的第(一)篇,更多shell教程请看:Linux Shell系列教程 想要学习linux,shell知识必不可少,今天就给大家来简单介绍下shell的基本知识. S ...

  10. 【Vjudge】P1989Subpalindromes(线段树)

    题目链接 水题一道,用线段树维护哈希值,脑补一下加减乱搞搞……注意细节就过了 一定注意细节…… #include<cstdio> #include<cstdlib> #incl ...