[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 题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小) ...
随机推荐
- cocos2dx day 3 - Chapter5 Scene
写在前面 越来越懒了,才3天,主要是cocos2dx官网的文章写的还是不是太完美,发现一段代码有个笔误,还有好几处写得不是很清楚的,所以有点泄气,不想继续读下去,不过为了我的第一款手游,一切困难都要先 ...
- sqlce中不支持sp_rename修改表名
The sp_rename procedure is not avialable in SQL CE! In Sql Server 2005 Management Studio you have to ...
- android studio 生成aar包并在其他工程引用 (导入)aar包
1.aar包是Android studio下打包android工程中src.res.lib后生成的aar文件,aar包导入其他android studio 工程后,其他工程可以方便引用源码和资源文件 ...
- 如何安装NodeJS到阿里云Centos (64位版本V5-7)
如何安装NodeJS到阿里云Centos (64位版本V5-7) (Centos与Red Hat® Enterprise Linux® / RHEL, Fedora属于一类) 1) 安装v0.10版 ...
- 自动化测试 using System.Windows.Automation;
frameworke3.0 及以上 using System.Windows.Automation; UIAutomationClient.dll UIAutomationClientsideProv ...
- 诺基亚N900使用技巧
一直都对 Linux 的掌上终端挺感冒的,最近从闲鱼上入后一台欧版的,开机进行评测和使用.以下经验仅供新手参考. 选择 N900 是有原因的,首先 N900 相对来说比较小巧(作为 MID,对比起手机 ...
- Javascript BOM对象
BOM是browser object model的缩写,简称浏览器对象模型. window对象(BOM的核心对象) 表示浏览器的一个实例,在浏览器中,window对象有着双重角色,它既是通过Javas ...
- javascript 字符串数组链接
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- .NET (二)委托第二讲:内置委托Func
在上一章节中,我们自己声明了一个委托: public delegate bool Cal(int num); 接受int参数,返回bool类型,目的是过滤集合中的 奇数 或者 偶数. .NET 为我们 ...
- 在js中怎么样选择互斥的相邻元素
在使用jquery中,我们通常会选择siblings()去选择相邻元素,使用eq()方法去匹配元素,使用index()获取对应元素的索引值,具体jquery代码如下: <style> *{ ...