题意:给你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. luogu1972 [SDOI2009]HH的项链

    莫队裸题还不带修改 #include <algorithm> #include <iostream> #include <cstdio> #include < ...

  2. javascript 内置日期转换方法

    var d = new Date(); console.log(d); // 输出:Mon Nov 04 2013 21:50:33 GMT+0800 (中国标准时间) console.log(d.t ...

  3. 26、android上跑apache的ftp服务

    一.为啥 在android设备跑ftp服务,在现场方便查看日志,目前就是这么用的. 二.前提: 从apache的官网下载依赖包:http://mina.apache.org/ftpserver-pro ...

  4. [uiautomator篇][8] 增加应用读取内置存储卡的权限

    1 要在androidmainfest.xml增加权限(这样之后,在设备上的权限才可以点击,不然是灰色) <uses-permission android:name="android. ...

  5. 九度oj 题目1355:扑克牌顺子

    题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他 ...

  6. 读《深入浅出Mysql》第二版,笔记

    买了本书,同时网上找了电子版 (My)SQL入门 DDL(Data Definition Languages) ||DESCRIBE tablename; DML(Data manipulation ...

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

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

  8. LaTeX:毕设模版设计问题1 各级标题编号设为黑体

    我用'titlesec'宏包设计各级标题格式指定字体为黑体,结果标题编号仍为Times New Roman .

  9. [USACO11DEC] Grass Planting (树链剖分)

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  10. Problem b(bzoj 2301)

    Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...