题意:给你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. Oracle数据库的日常使用命令

    1.     启动和关闭数据库 sqlplus /nolog; SQL >conn / as sysdba;(上面的两条命令相当于sqlplus ‘/as sysdba’) SQL >st ...

  2. C语言总结(5)

    一.函数的定义: 函数是一个完成特定工作的独立程序模块,包括库函数和自定义函数.例如:scanf(),printf()为库函数,cylinder(),fact()为自定义函数. 函数定义的一般形式: ...

  3. 为什么要用全文搜索引擎:全文搜索引擎 VS 数据库管理系统

    正文一:Full Text Search Engines vs. DBMS  发表于2009年 正文二:Elasticsearch - A High-Performance Full-Text Sea ...

  4. java异常处理及400,404,500错误处理

        java代码中经常碰到各种需要处理异常的时候,比如什么IOException  SQLException  NullPointException等等,在开发web项目中,遇到异常,我现在做的就 ...

  5. iOS----精品开源库-开发强力助攻

    30个精品iOS开源库,超强助攻 你不会想错过他们,真的. 我爱开源.  文章的尾部你会看到一个太长不看的版本——一个简单的列表,只有标题和到项目的链接.如果你发现这篇文章是有用的,把它和你的iOS开 ...

  6. 【bzoj4026】dC Loves Number Theory 可持久化线段树

    题目描述 dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源.  给定一个长度为 n的正整数序列A,有q次询问,每次询问一段区间内所有元素乘积的φ(φ(n ...

  7. Linux 终端操作之「I/O Redirection」

    I/O 重定向是在终端运行程序时很常用的技巧,但是我对它所知甚少.今天我在 DigitalOcean 上发现了一篇很好的 tutorial.这篇随笔记录一下我的心得体会和发现的一个问题. I/O re ...

  8. Golang指针

    学过C语言的老司机都知道,指针就是一个变量,用于存储另一个变量的内存地址. 那么什么是变量呢?在现代计算机体系结构中所有的需要执行的信息代码都需要存储在内存中,为了管理存储在内存的数据,内存是划分为不 ...

  9. P1260 工程规划 (差分约束)

    题目链接 Solution 差分约束. 差分约束似乎精髓就两句话: 当我们把不等式整理成 \(d[a]+w<=d[b]\) 时,我们求最长路. 整理成 \(d[a]+w>=d[b]\) 时 ...

  10. python输出字典中的中文

    如果不用本文指定的方法,会有如下报错: UnicodeDecodeError: 'utf8' codec can't decode byte 0xbf in position 2: invalid s ...