[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 题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小) ...
随机推荐
- Linux 开机启动
Linux开机启动(bootstrap) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 计算机开机是一个神秘的过程.我们只是 ...
- 逆向分析AHpack
从暑假开始逆向研究也有一个半月了,今晚分析了一个压缩壳,第一次脱离书本逆向一个程序,放上来纪念一下. 其实像这种壳完全可以esp定律秒掉的,之所以分析它,是因为我想知道所谓IAT修复具体是怎么个算法, ...
- SOLID rule in JAVA design.
Classes are the building blocks of your java application. If these blocks are not strong, your build ...
- U盘快捷方式中毒处理办法
这是网上某位大神的,对于我这个U盘总中毒的人真的很好用,太开心啦啦 http://blog.csdn.net/jzwong/article/details/51002568
- wcf 推送 与 广播
原文地址:http://blog.csdn.net/is_zhoufeng/article/details/7641234 ////wcf 接口 #region << 版 本 注 释 ...
- range for query
static void range_test(Args _args) { Query Query; QueryRun QueryRun ...
- pycharm激活码,拿走不谢
激活码 新下载pycharm编辑器以后,会遇到一个激活的问题,否则只能试用30天 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVO ...
- 解决sublime text3 文件名,小框框的办法
解决sublime text3 文件名,小框框的办法 之前一直都是用的英文命名的文件夹,到前几天才发现,用中文,来命名文件夹出现了乱码问题. 今天晚上,自己也在网上去百度了很多方案,好像大部分都不太有 ...
- 二模12day1解题报告
T1.笨笨与电影票(ticket) 有n个1和m个0,求每个数前1的个数都大于等于0的个数的排列数. 非常坑的一道题,推导过程很烦.首先求出所有排列数是 C(n+m,m),然后算不合法的个数. 假设存 ...
- Web后台技术趋势
今天使用Google Trend比较了一下服务器端的程序开发语言技术ASP/ASP.NET Core, PHP, Node.Js的变化趋势,发现一下特点. ASP.NET最近几年一直再下降. ASP和 ...