Sevenk Love Oimaster bzoj-2780 Spoj-8093

题目大意:给定$n$个大串和$m$次询问,每次给出一个字符串$s$询问在多少个大串中出现过。

注释:$1\le n\le 10^4$,$1\le q\le 6\cdot 10^4$,$the\ total\ length\ of\ n\ strings\ \le 10^5$,

$the\ total\ length\ of\ q\ question\ strings\le 3.6\times 10^5$。


想法:广义后缀自动机

先对$n$个串建立广义后缀自动机。

后缀自动机上每个节点记录下在多少个串串里出现过,记为$cnt_i$。

然后对于每个询问串,沿着$trans$指针走到当前的$now$节点,输出$cnt_{now}$即可。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#define N 200010
using namespace std;
int n,m,last=1,tot=1,tr[N][26],fa[N],len[N],cnt[N],vis[N];
string s[N],ss;
void update(int c)
{
int v=last,u=++tot;
last=u;
len[u]=len[v]+1;
while(v&&!tr[v][c]) tr[v][c]=u,v=fa[v];
if(!v) fa[u]=1;
else
{
int x=tr[v][c];
if(len[x]==len[v]+1) fa[u]=x;
else
{
int y=++tot;
memcpy(tr[y],tr[x],sizeof tr[y]);
fa[y]=fa[x]; fa[x]=fa[u]=y;
len[y]=len[v]+1;
while(v&&tr[v][c]==x) tr[v][c]=y,v=fa[v];
}
}
}
int main()
{
ios::sync_with_stdio(false);
int n,m;
cin >> n >> m ;
for(int i=1;i<=n;i++)
{
cin >> s[i] ;
int l=s[i].length();
last=1;
for(int j=0;j<l;j++) update(s[i][j]-'a');
}
for(int i=1;i<=n;i++)
{
int l=s[i].length(),now=1;
for(int j=0;j<l;j++)
{
now=tr[now][s[i][j]-'a'];
int t=now;
while(t&&vis[t]!=i)
{
cnt[t]++;
vis[t]=i;
t=fa[t];
}
}
}
for(int i=1;i<=m;i++)
{
cin >> ss ;
int l=ss.length(),now=1;
for(int j=0;j<l;j++) now=tr[now][ss[j]-'a'];
printf("%d\n",cnt[now]);
}
return 0;
}

小结:对后缀自动机的理解好浅啊...

[bzoj2780][Spoj8093]Sevenk Love Oimaster_广义后缀自动机的更多相关文章

  1. BZOJ.2780.[SPOJ8093]Sevenk Love Oimaster(广义后缀自动机)

    题目链接 \(Description\) 给定n个模式串,多次询问一个串在多少个模式串中出现过.(字符集为26个小写字母) \(Solution\) 对每个询问串进行匹配最终会达到一个节点,我们需要得 ...

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

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

  3. bzoj 3277 串 && bzoj 3473 字符串 && bzoj 2780 [Spoj]8093 Sevenk Love Oimaster——广义后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3277 https://www.lydsy.com/JudgeOnline/problem.p ...

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

    给定n个串m个询问,问每个串在n个串多少个串中出现了. 构建广义后缀自动机,(就是把所有字符串的后缀自动机合并起来)其实只需要add的时候注意一下就可以了. 然后对于每一个串,跑一边匹配,到达了now ...

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

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

  6. 【BZOJ2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机

    [BZOJ2780][Spoj]8093 Sevenk Love Oimaster Description Oimaster and sevenk love each other.     But r ...

  7. BZOJ2780:[SPOJ8093]Sevenk Love Oimaster(广义SAM)

    Description Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXu ...

  8. BZOJ2780 [Spoj]8093 Sevenk Love Oimaster 【广义后缀自动机】

    题目 Oimaster and sevenk love each other. But recently,sevenk heard that a girl named ChuYuXun was dat ...

  9. 【BZOJ2780】Sevenk Love Oimaster【广义后缀自动机】

    题意 给出你n个字符串和q个查询,每个查询给出一个字符串s,对于每个查询你都要输出这个字符串s在上面多少个字符串中出现过. 分析 广义后缀自动机的裸题.建好SAM以后再跑一遍得到每个状态的ocu和la ...

随机推荐

  1. java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()

    原因:hibernate-jpa-2.0-api-1.0.0.Final.jar.ejb3-persistence.jar中的javax.persistence与javaEE 5 Librares中的 ...

  2. Winform用Post方式打开IE

    1.主要实现Code void OpenNewIe(string url, string postData)///url是要post的网址,postData是要传入的参数 { if (ie != nu ...

  3. uva1380 A Scheduling Problem

    按紫书来注意这道题的题目给了很大的方便,就相当于验证k是不是答案,不是的话就是k+1 #include<iostream> #include<string> #include& ...

  4. https://www.runoob.com/python/python-variable-types.html

    https://www.runoob.com/python/python-variable-types.html

  5. Chrome浏览器安装React developer tools

    1. 到 https://github.com/facebook/react-devtools 下载 react-devtools 2. 进入 react-devtools 目录 运行命令  npm ...

  6. 按名字寻找文件和文件夹 find命令

    find <指定目录> <指定条件> <指定动作> find /home/bnrc/py-faster-rcnn/caffe-fast-rcnn/ -name 'd ...

  7. scrapy example

    scrapy example scrapy with pycharm import win32api 出现ImportError: DLL load failed 错误的解决方法 pip instal ...

  8. oc学习

    http://www.cnblogs.com/qingyuan/p/3524678.html

  9. java 生成二维码工具

    二维码生成 Gitee:https://gitee.com/search?utf8=%E2%9C%93&search=qrext4j&group_id=&project_id= ...

  10. ubuntu修改网卡名称ensX为eth0

    1.sudo nano /etc/default/grub 找到GRUB_CMDLINE_LINUX="" 改为GRUB_CMDLINE_LINUX="net.ifnam ...