题目链接

广义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(后缀自动机)的更多相关文章

  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插入一个串之后,插入新的串的时候,要把last重新调到1的位置,共用一些节点. 这个题我们首先要预处理出来每个状态被多少个串共用.挺暴力的 ...

  4. 洛谷P4770 [NOI2018]你的名字 [后缀自动机,线段树合并]

    传送门 思路 按照套路,直接上后缀自动机. 部分分:\(l=1,r=|S|\) 首先把\(S\)和\(T\)的后缀自动机都建出来. 考虑枚举\(T\)中的右端点\(r\),查询以\(r\)结尾的串最长 ...

  5. 【洛谷 P3975】 [TJOI2015]弦论(后缀自动机)

    题目链接 建出后缀自动机. T=0,每个子串算一次,否则每个子串算该子串的\(endpos\)集合大小次. 用\(f[i]\)表示结点\(i\)表示的\(endpos\)集合大小,则\(f[i]\)为 ...

  6. SP8093 JZPGYZ - Sevenk Love Oimaster(SAM)

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

  7. 【洛谷 P4248】 [AHOI2013]差异(后缀自动机)

    题目链接 \[ans=\sum_{1<=i<j<=n}len(T_i)+len(T_j)-2*lcp(T_i,T_j)\] 观察这个式子可以发现,前面两个\(len\)是常数,后面的 ...

  8. BZOJ 2780 Sevenk Love Oimaster (后缀自动机+树状数组+dfs序+离线)

    题目大意: 给你$n$个大串和$m$个询问,每次给出一个字符串$s$询问在多少个大串中出现过 好神的一道题 对$n$个大串建出广义$SAM$,建出$parent$树 把字符串$s$放到$SAM$里跑, ...

  9. 【洛谷 P3804】 【模板】后缀自动机

    题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...

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

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

随机推荐

  1. Ecms7.5版CK编辑器保留word格式如何修改

    7.5版的编辑器默认会清除多余的word代码,如果要保留word格式怎么修改? CKeditor编辑器默认复制会清除多余word代码,如果要保留word格式可以按下面修改配置: 修改 /e/admin ...

  2. Goroutine调度器

    前言 并发(并行)一致都是编程语言的核心主题,不同于其他语言,例如C/C++语言用户序自行借助pthread创建线程,Golang天然就给出了并发解决方案:goroutine. Goroutine 写 ...

  3. python中pygame游戏打包为exe文件

    pyinstaller打包游戏的方法: 1.在命令窗口安装pyinstaller ->pip install pyinstaller 2.查看安装的版本信息 pyinstaller -v 3.进 ...

  4. ubuntu18 maven

    user1@user1-ThinkPad-W540:~$ user1@user1-ThinkPad-W540:~$ sudo mkdir /opt/maven[sudo] password for u ...

  5. Pycharm连接远程服务器并进行代码上传+远程调试

    前提:需要有一个远程服务器,知道他的ip.port.user.password 一.连接远程服务器 进入配置页面 Pycharm菜单栏,如下图所示,依次点击 Tools -> Deploymen ...

  6. server2008r2 安装CentOS

    一:安装CentOS 二:配置虚拟网络: 三:设置创建的虚拟机使用刚才创建的网卡: 四:运行CentOs,输入用户:root  密码:root,登录后输入: dhclient   自动获取IP ip ...

  7. [LeetCode] 133. Clone Graph 克隆无向图

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  8. node 单例

    ScriptManager.getInstance = function () { if (_instance != null) { return _instance; } else { return ...

  9. System.gc()介绍

    System.gc()用于垃圾收集器,调用垃圾收集器将回收未使用的 System.gc()进行回收的准则: 回收没有被任何可达变量指向的对象 JDK实现 public static void gc() ...

  10. layoutSubviews在什么情况下调用

    可以使用layoutSubviews修改UI: 1.init初始化不会触发layoutSubviews 但是是用initWithFrame 进行初始化时,当rect的值不为CGRectZero时,也会 ...