【洛谷 SP8093】 JZPGYZ - Sevenk Love Oimaster(后缀自动机)
题目链接
广义sam。。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1000010;
struct SAM{
int ch[26];
int len, fa;
}sam[MAXN << 1];
int las = 1, cnt = 1, v[MAXN << 1], sz[MAXN << 1];
int len[MAXN], tot, n, m;
inline void add(int c){
int p = las; int np = las = ++cnt;
sam[np].len = sam[p].len + 1;
for(; p && !sam[p].ch[c]; p = sam[p].fa) sam[p].ch[c] = np;
if(!p) sam[np].fa = 1;
else{
int q = sam[p].ch[c];
if(sam[q].len == sam[p].len + 1) sam[np].fa = q;
else{
int nq = ++cnt; sam[nq] = sam[q];
sam[nq].len = sam[p].len + 1;
sam[q].fa = sam[np].fa = nq;
for(; p && sam[p].ch[c] == q; p = sam[p].fa) sam[p].ch[c] = nq;
}
}
}
char a[MAXN];
void update(int x, int y){
for(; x && v[x] != y; x = sam[x].fa){
v[x] = y; ++sz[x];
}
}
int ans;
int main(){
scanf("%d%d", &n, &m); getchar();
for(int i = 1; i <= n; ++i){
las = 1;
char ch;
while((ch = getchar()) != '\n') add(a[++tot] = ch - 'a'), ++len[i];
}tot = 0;
for(int i = 1; i <= n; ++i)
for(int j = 1, x = 1; j <= len[i]; ++j)
update(x = sam[x].ch[a[++tot]], i);
for(int i = 1; i <= m; ++i){
ans = 0; int p = 1;
scanf("%s", a);
int len = strlen(a);
for(int j = 0; j < len; ++j)
if(sam[p].ch[a[j] - 'a'])
p = sam[p].ch[a[j] - 'a'];
else{
ans = -1;
break;
}
if(ans) ans = 0;
else ans = sz[p];
printf("%d\n", ans);
}
return 0;
}
【洛谷 SP8093】 JZPGYZ - Sevenk Love Oimaster(后缀自动机)的更多相关文章
- SP8093 JZPGYZ - Sevenk Love Oimaster 解题报告
SP8093 JZPGYZ - Sevenk Love Oimaster 题目大意 给定\(n(n\le 10000)\)个模板串,以及\(m(m\le 60000)\)个查询串(模板串总长\(\le ...
- SP8093 JZPGYZ - Sevenk Love Oimaster(广义后缀自动机)
题意 题目链接 Sol 广义后缀自动机板子题..和BZOJ串那个题很像 首先建出询问串的SAM,然后统计一下每个节点被多少个串包含 最后直接拿询问串上去跑就行了 #include<bits/st ...
- SP8093 JZPGYZ - Sevenk Love Oimaster
传送门 广义后缀自动机-- 其实也不是很难理解,就是每次SAM插入一个串之后,插入新的串的时候,要把last重新调到1的位置,共用一些节点. 这个题我们首先要预处理出来每个状态被多少个串共用.挺暴力的 ...
- 洛谷P4770 [NOI2018]你的名字 [后缀自动机,线段树合并]
传送门 思路 按照套路,直接上后缀自动机. 部分分:\(l=1,r=|S|\) 首先把\(S\)和\(T\)的后缀自动机都建出来. 考虑枚举\(T\)中的右端点\(r\),查询以\(r\)结尾的串最长 ...
- 【洛谷 P3975】 [TJOI2015]弦论(后缀自动机)
题目链接 建出后缀自动机. T=0,每个子串算一次,否则每个子串算该子串的\(endpos\)集合大小次. 用\(f[i]\)表示结点\(i\)表示的\(endpos\)集合大小,则\(f[i]\)为 ...
- SP8093 JZPGYZ - Sevenk Love Oimaster(SAM)
/* 打模板题啊 每个串影响到的集合直接枚举跳parent处理即可 */ #include<cstdio> #include<algorithm> #include<cs ...
- 【洛谷 P4248】 [AHOI2013]差异(后缀自动机)
题目链接 \[ans=\sum_{1<=i<j<=n}len(T_i)+len(T_j)-2*lcp(T_i,T_j)\] 观察这个式子可以发现,前面两个\(len\)是常数,后面的 ...
- BZOJ 2780 Sevenk Love Oimaster (后缀自动机+树状数组+dfs序+离线)
题目大意: 给你$n$个大串和$m$个询问,每次给出一个字符串$s$询问在多少个大串中出现过 好神的一道题 对$n$个大串建出广义$SAM$,建出$parent$树 把字符串$s$放到$SAM$里跑, ...
- 【洛谷 P3804】 【模板】后缀自动机
题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...
- BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )
全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries ...
随机推荐
- 分享一些好用的 Chrome 扩展
阅读本文大概需要 2.8 分钟. 前言 使用浏览器扩展程序可以使你的工作效率提高数倍不止,那么下面我就向大家分享一下我日常使用的扩展,可能大多数扩展大家都已经在使用了,不过也难免有一两个是你不知道的. ...
- Remind Me
创建2d人物:live2d 创建3d人物:adobe fuse
- 从宿主机直接进入docker容器的网络空间
Docker dns nameserver 也是进入容器网络空间,监听53端口,但它通过iptable把端口映射到宿主机上,处理DNS请求的进程就在宿主机上. how does Docker Embe ...
- 范仁义html+css课程---3、图片和超链接
范仁义html+css课程---3.图片和超链接 一.总结 一句话总结: img标签是图片标签,定义 HTML 页面中的图像 a标签是超链接标签,用于从一个页面链接到另一个页面. 1.img标签要点? ...
- Predicting effects of noncoding variants with deep learning–based sequence model | 基于深度学习的序列模型预测非编码区变异的影响
Predicting effects of noncoding variants with deep learning–based sequence model PDF Interpreting no ...
- easyui datagrid怎么动态获取表头的列名及显示名称
说明:目前使用easyui combobox多选属性,绑定的数据源是来自datagrid的表头的列名及显示名称 处理方法: //获取冻结的数据源并返回key,value格式数据 var GetFroz ...
- 深入学习c++--多线程编程(三)thread的两种死法
1. 生成了一个线程,需要告诉编译器是否管理 必须告诉编译器是不管理还是管理,否则直接down了 #include <iostream> #include <thread> # ...
- LeetCode_409. Longest Palindrome
409. Longest Palindrome Easy Given a string which consists of lowercase or uppercase letters, find t ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] 504. Base 7 基数七
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...