题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384

题意:函数f(A, B)定义:A、B为字符串,f(A, B)为A中有多少个不同的B(ex:f("ababa", "aba")==2   f("ababa", "bab")==1),现在给你一组A串,一组B串,问你对每个A串的是多少

解:ac自动机模板题,可以把A串用'z'+1,连成一个串处理起来比较方便

 /*
* Problem: hdu5384 Danganronpa
* Author: SHJWUDP
* Created Time: 2015/8/13 星期四 14:38:23
* File Name: 1006.cpp
* State: Accepted
* Memo: ac自动机
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; const int MaxA=1e5+;
const int MaxB=7e5+; const int SIGMA_SIZE=; struct AhoCorasickAutomata {
int ch[MaxB][SIGMA_SIZE];
int val[MaxB];
int sz;
int f[MaxB], last[MaxB]; int newNode() {
memset(ch[sz], , sizeof(ch[sz]));
val[sz]=;
return sz++;
} void init() {
sz=;
newNode();
} int id(char c) {
return c-'a';
} void insert(char* p) {
int u=;
while(*p) {
int c=id(*p++);
if(!ch[u][c]) ch[u][c]=newNode();
u=ch[u][c];
}
val[u]++;
} void getFail() {
queue<int> Q;
f[]=;
for(int c=; c<SIGMA_SIZE; c++) {
int u=ch[][c];
if(u) { f[u]=; Q.push(u); last[u]=; }
} while(!Q.empty()) {
int r=Q.front(); Q.pop();
for(int c=; c<SIGMA_SIZE; c++) {
int u=ch[r][c];
if(!u) { ch[r][c]=ch[f[r]][c]; continue; }
Q.push(u);
int v=f[r];
while(v && !ch[v][c]) v=f[v];
f[u]=ch[v][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
} long long dealAns(int u) {
long long res=;
while(u) {
res+=val[u];
// val[u]=0;
u=last[u];
}
return res;
} void find(char * T, long long * ans) {
int u=;
int pos=;
long long sum=, ltsum=;
for(int i=; T[i]; i++) {
int c=id(T[i]);
u=ch[u][c];
if(val[u]) sum+=dealAns(u);
else if(last[u]) sum+=dealAns(last[u]);
if(T[i]=='z'+) {
ans[pos++]+=sum-ltsum;
ltsum=sum;
}
}
}
} ac; int n, m;
char str[MaxB];
long long ans[MaxA];
char gogogo[MaxA];
int main() {
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
int len=;
for(int i=; i<n; i++) {
scanf("%s", str+len);
while(str[len]) len++;
str[len++]='z'+;
}
str[len]='\0';
ac.init();
for(int i=; i<m; i++) {
scanf("%s", gogogo);
ac.insert(gogogo);
}
ac.getFail();
memset(ans, , sizeof(ans));
ac.find(str, ans);
for(int i=; i<=n; i++) {
printf("%I64d\n", ans[i]);
}
}
return ;
}

[2015hdu多校联赛补题]hdu5384 Danganronpa的更多相关文章

  1. [2015hdu多校联赛补题]hdu5302 Connect the Graph

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5302 题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连.现在 ...

  2. [2015hdu多校联赛补题]hdu5301 Buildings

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 题目大意:给你一块由1x1方格组成的矩形区域,其中有且仅有一个坏块,现在你要在上面建矩形的房子, ...

  3. [2015hdu多校联赛补题]hdu5378 Leader in Tree Land

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...

  4. [2015hdu多校联赛补题]hdu5372 Segment Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始 0操作 0 ...

  5. [2015hdu多校联赛补题]hdu5371 Hotaru's problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...

  6. [2015hdu多校联赛补题]hdu5303 Delicious Apples

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...

  7. [2015hdu多校联赛补题]hdu5299 Circles Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5299 题意: 在欧几里得平面上有n个圆,圆之间不会相交也不会相切,现在Alice和Bob玩游戏,两人轮 ...

  8. [2015hdu多校联赛补题]hdu5348 MZL's endless loop

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...

  9. [2015hdu多校联赛补题]hdu5324 Boring Class

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5324 题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小) ...

随机推荐

  1. [转]SQL Server 连接串关键字别名

    转自:http://stackoverflow.com/questions/3077412/what-is-the-difference-between-trusted-connection-and- ...

  2. 1不等于1?numeric、decimal、float 和 real 数据类型的区别

    大家有没有在SQL中遇见1不等于1(1<>1)的情形!?下面会有一个例子演示这个情形. 先简单介绍一下标题中的四种数值数据类型. 在T-SQL中,numeric和decimal是精确数值数 ...

  3. Autoit 在word中绘图

    没有时间整理,直接看参考网址: http://www.autoitx.com/thread-257-1-1.html

  4. java多线程处理

    package com.copyFile; import java.io.BufferedReader;import java.io.File;import java.io.FileReader;im ...

  5. Mysql字符类型比较

    一. binary和char比较: binary 字节为单位,char字符为单位,字符占几个字节取决于字符集 binary  比较规则基于字节值,char基于字符,即使是_bin的比较规则 范围都0- ...

  6. MySQL For Windows Zip解压版安装

    前言 Windows 下 MySQL 有msi和zip解压安装版两种,而zip版只需解压并做简单配置后就能使用,我个人比较喜欢这种方式. 注意我们这里说的MySQL是指MySQL服务器,有很多初学的同 ...

  7. 用ARCGIS配出一张DEM专题图

    专题图是指突出而尽可能完善.详尽地表达制图区内的一种或几种自然或社会经济要素的地图.专题图的制图领域宽广,凡具有空间属性的信息数据都可以用其来表示.由于DEM描述的是地面高程信息,它在测绘.水文.气象 ...

  8. mysql 模糊查询语句比较(LIKE、instr、locate、find_in_set、position)

    大家都知道mysql 模糊查询的常用方法是LIKE 但这个语句查询效率很慢,那么有没有比较好的方法呢,下面本人测试了几个语句 测试数据800条左右 1,

  9. 3、python,read(),readlines()和readline()

    我们谈到"文本处理"时,我们通常是指处理的内容. Python 对文件对象的操作提供了三个"读"方法: .read()..readline() 和 .readl ...

  10. Hibernate 中出现 users is not mapped 问题 (转)

    今天晚上自己试着用Hibernate去搭建一个Web工程,然后去实现一个简单的登录.         通过Hibernate 做查询操作的时候总是报出这样的错:                    ...