传送门

广义后缀自动机……

其实也不是很难理解,就是每次SAM插入一个串之后,插入新的串的时候,要把last重新调到1的位置,共用一些节点。

这个题我们首先要预处理出来每个状态被多少个串共用。挺暴力的就是每次把节点染色,如果节点没被染色就给他染一下,然后记录当前节点又被共用了一次。

最后跑的时候和匹配是一样的,每次输出末尾节点共用次数即可。

#include<bits/stdc++.h>
#define rep(i,a,n) for(register int i = a;i <= n;i++)
#define per(i,n,a) for(register int i = n;i >= a;i--)
#define enter putchar('\n')
#define pr pair<int,int>
#define mp make_pair
#define fi first
#define sc second
using namespace std;
typedef long long ll;
const int M = 100005;
const int N = 10000005;
const int INF = 2147483647; int read()
{
int ans = 0,op = 1;
char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
while(ch >='0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
return ans * op;
} string s[M],t;
int n,m,k,times[M<<1],col[M<<1]; struct Suffix
{
int last,cnt,ch[M<<1][26],fa[M<<1],l[M<<1];
void extend(int c)
{
int p = last,np = ++cnt;
last = cnt,l[np] = l[p] + 1;
while(p && !ch[p][c]) ch[p][c] = np,p = fa[p];
if(!p) {fa[np] = 1;return;}
int q = ch[p][c];
if(l[q] == l[p] + 1) fa[np] = q;
else
{
int nq = ++cnt;
l[nq] = l[p] + 1,memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq] = fa[q],fa[q] = fa[np] = nq;
while(ch[p][c] == q) ch[p][c] = nq,p = fa[p];
}
}
void cal(string b,int q)
{
int cur = 1,h = b.length();
rep(j,0,h-1)
{
cur = ch[cur][b[j] - 'a'];
int t = cur;
while(t && col[t] != q) times[t]++,col[t] = q,t = fa[t];
}
}
void match(string b)
{
int cur = 1;
rep(i,0,k-1) cur = ch[cur][b[i]-'a'];
printf("%d\n",times[cur]);
}
}SAM; int main()
{
n = read(),m = read();
SAM.cnt = 1;
rep(i,1,n)
{
cin >> s[i],k = s[i].length();
SAM.last = 1;
rep(j,0,k-1) SAM.extend(s[i][j] - 'a');
}
rep(i,1,n) SAM.cal(s[i],i);
while(m--) cin >> t,k = t.length(),SAM.match(t);
return 0;
}

SP8093 JZPGYZ - Sevenk Love Oimaster的更多相关文章

  1. SP8093 JZPGYZ - Sevenk Love Oimaster 解题报告

    SP8093 JZPGYZ - Sevenk Love Oimaster 题目大意 给定\(n(n\le 10000)\)个模板串,以及\(m(m\le 60000)\)个查询串(模板串总长\(\le ...

  2. SP8093 JZPGYZ - Sevenk Love Oimaster(广义后缀自动机)

    题意 题目链接 Sol 广义后缀自动机板子题..和BZOJ串那个题很像 首先建出询问串的SAM,然后统计一下每个节点被多少个串包含 最后直接拿询问串上去跑就行了 #include<bits/st ...

  3. SP8093 JZPGYZ - Sevenk Love Oimaster(SAM)

    /* 打模板题啊 每个串影响到的集合直接枚举跳parent处理即可 */ #include<cstdio> #include<algorithm> #include<cs ...

  4. 【洛谷 SP8093】 JZPGYZ - Sevenk Love Oimaster(后缀自动机)

    题目链接 广义sam.. #include <cstdio> #include <cstring> #include <algorithm> using names ...

  5. SPOJ 8093 JZPGYZ - Sevenk Love Oimaster

    思路 可以用复杂度不对的做法水过去 相当于求parent树子树中的颜色种数,可以离线后树状数组(HH的项链,询问右端点排序之后维护last),dsu on tree,莫队都可以 但是也可以记录每个点上 ...

  6. SPOJ8093【JZPGYZ - Sevenk Love Oimaster】

    怎么全是广义后缀自动机,我\(AC\)自动机不服 这道题可以使用的算法很多,\(SA\)或者\(SAM\)应该都可以 但是我都不会 但是这毕竟是一个多串匹配问题,\(AC\)自动机还是可以刚一刚的 我 ...

  7. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster [广义后缀自动机]

    JZPGYZ - Sevenk Love Oimaster     Oimaster and sevenk love each other.       But recently,sevenk hea ...

  8. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )

    全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries ...

  9. 【BZOJ2780】【SPOJ】Sevenk Love Oimaster(后缀自动机)

    [BZOJ2780][SPOJ]Sevenk Love Oimaster(后缀自动机) 题面 BZOJ 洛谷 题解 裸的广义后缀自动机??? 建立广义后缀自动机建立出来之后算一下每个节点被几个串给包括 ...

随机推荐

  1. py3中的文字编码

    Python3 中字符的类型只有两种: str: 编码过的 unicode 文本字符 bytes: 编码前的字节序列

  2. 百科知识 华为手机P7如何更换电池

    参考下面 教程 https://item.jd.com/3265516.html  

  3. 用Camshift算法对指定目标进行跟踪

    原理 Camshift算法是Continuously Adaptive Mean Shift algorithm的简称. 它是一个基于MeanSift的改进算法.它首次由Gary R.Bradski等 ...

  4. B树的生成

    B树的生成 flyfish 2015-7-19 从空树開始构建一棵B树 逐个插入keyword 规则: 除根结点之外的全部非终端结点至少有⌈m/2⌉棵子树,所以keyword的个数必须 n为keywo ...

  5. openshifit 安装 redis

    http://blog.csdn.net/lsx991947534/article/details/48860537 http://blog.csdn.net/aguangg_6655_la/arti ...

  6. 用python模拟TCP3次握手连接及发送数据

    源码如下: from scapy.all import * import logging logging.getLogger('scapy.runtime').setLevel(logging.ERR ...

  7. Mapreduce实战:序列化与反序列化 int,int[],string[][]

    最新一期<中国IT产业发展报告>在2016中国(深圳)IT领袖峰会上正式发布,数字中国联合会常务理事李颖称.中国IT产业完毕了从要素驱动向效率驱动的过渡,眼下正在由效率驱动向创新驱动发展. ...

  8. Codeforces Round #243 (Div. 1)——Sereja and Squares

    题目链接 题意: 给n个点,求能组成的正方形的个数. 四边均平行与坐标轴 大神的分析: 经典题 我们考虑每一种x坐标,显然仅仅有<= sqrt{N}个x坐标出现了> sqrt{N}次,我们 ...

  9. python 修饰符(转载)

    首先一个修饰符的实例: #!/usr/bin/env python def run(fn): def do_hello(): print "begin..." fn() print ...

  10. 九度OJ 1140:八皇后 (八皇后问题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:795 解决:494 题目描述: 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * ...