[2015hdu多校联赛补题]hdu5384 Danganronpa
题目链接: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的更多相关文章
- [2015hdu多校联赛补题]hdu5302 Connect the Graph
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5302 题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连.现在 ...
- [2015hdu多校联赛补题]hdu5301 Buildings
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 题目大意:给你一块由1x1方格组成的矩形区域,其中有且仅有一个坏块,现在你要在上面建矩形的房子, ...
- [2015hdu多校联赛补题]hdu5378 Leader in Tree Land
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...
- [2015hdu多校联赛补题]hdu5372 Segment Game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始 0操作 0 ...
- [2015hdu多校联赛补题]hdu5371 Hotaru's problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...
- [2015hdu多校联赛补题]hdu5303 Delicious Apples
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...
- [2015hdu多校联赛补题]hdu5299 Circles Game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5299 题意: 在欧几里得平面上有n个圆,圆之间不会相交也不会相切,现在Alice和Bob玩游戏,两人轮 ...
- [2015hdu多校联赛补题]hdu5348 MZL's endless loop
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...
- [2015hdu多校联赛补题]hdu5324 Boring Class
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5324 题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小) ...
随机推荐
- 通过seekBar改变图片的透明度
作者:堕落的天使 对应的图片 activity_main.xml(代码) <RelativeLayout xmlns:android="http://schemas.android.c ...
- 第一篇英文短文《It All Starts With A Dream》
http://www.ximalaya.com/#/17209107/sound/6883165 Dreaming. Do you or don’t you? Do you dream about t ...
- Xilinx FPGA全局时钟和全局时钟资源的使用方法
对FPGA的全局时钟了解不多,遂转载一篇文档: http://xilinx.eetop.cn/?action-viewnews-itemid-42 目前,大型设计一般推荐使用同步时序电路.同步时序电路 ...
- TFS 自动同步Server 端文件的批处理命令
TFS 自动同步Server 端文件的批处理命令 目前在我们组的工作中很多时候需要将TFS上Server端的代码自动无人值守的同步到本地中来, 找到了一些解决方案的资料http://bbs.scmro ...
- android下拉框
XML: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...
- 安装完Pydev却无法创建Python工程
为了方便以后工作,今天在ADT里面安装了Pydev(http://pydev.org/updates),可是安装完之后,新建项目的时候却找不到Pydev,perference中也没有. 紧接着尝试安装 ...
- python多进程提高cpu利用率
cpu参数: 1个物理cpu,2个逻辑cpu(超线程),单核 具体 http://blog.csdn.net/dba_waterbin/article/details/8644626 物理CPU. ...
- session的常用方法。
void setAttribute(String attribute, Object value) 设置Session属性.value参数可以为任何Java Object.通常为Java Bean.v ...
- 搭建fedora开发环境 common lisp, c++, go
第三方软件库: http://download1.rpmfusion.org/free/fedora/releases/25/Everything/x86_64/os/repoview/index.h ...
- neutron debug
neutron port-list neutron port-delete neutron floatingip-list neutron floatingip-delete