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

https://www.lydsy.com/JudgeOnline/problem.php?id=3473

广义后缀自动机:https://www.cnblogs.com/HocRiser/p/9580478.html

像 Trie 树一样处理了重复节点;

基数排序后DP,f 数组求的直接是这个点及其祖先的答案;

开 2e5 就可以,因为每次加入一个字符最多新增2个点。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const xn=2e5+;
int n,K,cnt=,go[xn][],len[xn],fa[xn],vis[xn],tot[xn],lst;
ll f[xn];
string s[xn];
int work(int p,int w)
{
int nq=++cnt,q=go[p][w]; len[nq]=len[p]+;
memcpy(go[nq],go[q],sizeof go[q]);
fa[nq]=fa[q]; fa[q]=nq;
for(;p&&go[p][w]==q;p=fa[p])go[p][w]=nq;
return nq;
}
int ext(int p,int w)
{
if(go[p][w])
{
int q=go[p][w];
if(len[q]==len[p]+)return q; return work(p,w);
}
int np=++cnt; len[np]=len[p]+;
for(;p&&!go[p][w];p=fa[p])go[p][w]=np;
if(!p)fa[np]=;
else
{
int q=go[p][w];
if(len[q]==len[p]+)fa[np]=q;
else fa[np]=work(p,w);
}
return np;
}
int tax[xn],q[xn],l[xn];
void rsort()
{
for(int i=;i<=cnt;i++)tax[len[i]]++;
for(int i=;i<=cnt;i++)tax[i]+=tax[i-];
for(int i=cnt;i;i--)q[tax[len[i]]--]=i;
}
char dc[xn];
int main()
{
scanf("%d%d",&n,&K);
for(int i=;i<=n;i++)
{
scanf("%s",dc); s[i]=string(dc); l[i]=strlen(dc); lst=;
for(int j=;j<l[i];j++)lst=ext(lst,s[i][j]-'a');
}
for(int i=;i<=n;i++)
{
int nw=;
for(int j=;j<l[i];j++)
{
nw=go[nw][s[i][j]-'a'];
for(int p=nw;p&&vis[p]!=i;p=fa[p])vis[p]=i,tot[p]++;
}
}
rsort();
for(int i=,x;i<=cnt;i++)
f[x=q[i]]=f[fa[x]]+(tot[x]>=K?len[x]-len[fa[x]]:);//
for(int i=;i<=n;i++)
{
ll ans=; int nw=;
for(int j=;j<l[i];j++)
nw=go[nw][s[i][j]-'a'],ans+=f[nw];
printf("%lld ",ans);
}
puts(""); return ;
}

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

几乎完全一样,但忘记写 lst=1 呆了半小时...

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const xn=2e5+;
int n,m,cnt=,fa[xn],l[xn],go[xn][],len[xn],tot[xn],vis[xn],lst;
char dc[xn]; string s[xn];
int work(int p,int w)
{
int nq=++cnt,q=go[p][w]; len[nq]=len[p]+;
memcpy(go[nq],go[q],sizeof go[q]);
fa[nq]=fa[q]; fa[q]=nq;
for(;p&&go[p][w]==q;p=fa[p])go[p][w]=nq;
return nq;
}
int ext(int p,int w)
{
if(go[p][w])
{
int q=go[p][w];
if(len[q]==len[p]+)return q; return work(p,w);
}
int np=++cnt; len[np]=len[p]+;
for(;p&&!go[p][w];p=fa[p])go[p][w]=np;
if(!p)fa[np]=;
else
{
int q=go[p][w];
if(len[q]==len[p]+)fa[np]=q;
else fa[np]=work(p,w);
}
return np;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%s",dc); l[i]=strlen(dc); s[i]=string(dc); lst=;//lst=1!!
for(int j=;j<l[i];j++)lst=ext(lst,s[i][j]-'a');
}
for(int i=;i<=n;i++)
{
int nw=;
for(int j=;j<l[i];j++)
{
nw=go[nw][s[i][j]-'a'];
for(int p=nw;p&&vis[p]!=i;p=fa[p])vis[p]=i,tot[p]++;
}
}
for(int i=;i<=m;i++)
{
scanf("%s",dc); int lth=strlen(dc),nw=;
for(int j=;j<lth;j++)nw=go[nw][dc[j]-'a'];
printf("%d\n",tot[nw]);
}
return ;
}

bzoj 3277 & bzoj 3473,bzoj 2780 —— 广义后缀自动机的更多相关文章

  1. BZOJ 3473: 字符串 (广义后缀自动机)

    /* 广义后缀自动机, 每次加入维护 该right集合的set, 然后可以更新所有的parent,最终能够出现在k个串中right集合也就是set大小大于等于k的部分 这样的话就给了我们要跳的节点加了 ...

  2. BZOJ 4566 [Haoi2016]找相同字符 ——广义后缀自动机

    建立广义后缀自动机. 然后统计子树中的siz,需要分开统计 然后对(l[i]-l[fa[i]])*siz[i][0]*siz[i][1]求和即可. #include <cstdio> #i ...

  3. BZOJ 2806 Luogu P4022 [CTSC2012]Cheat (广义后缀自动机、DP、二分、单调队列)

    题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=2806 (luogu) https://www.luogu.org/pro ...

  4. bzoj 4566 [Haoi2016]找相同字符——广义后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4566 每个后缀结尾处 ct[ ] = 1 ,按拓扑序 dp 一下就能求出 right 集合的 ...

  5. 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 ...

  6. BZOJ 3277/3473 广义后缀自动机

    说实话没啥难的. 建一棵广义后缀自动机,暴力自底向上更新即可. 时间复杂度非常玄学,但据说是可以过的. 要注意每个串中相同的子串的贡献是都要加进去的,开始因为这个被坑了好久 QAQ Code: #in ...

  7. BZOJ 3277 串 (广义后缀自动机)

    3277: 串 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 309 Solved: 118 [Submit][Status][Discuss] De ...

  8. BZOJ 3473: 字符串 [广义后缀自动机]

    3473: 字符串 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 354  Solved: 160[Submit][Status][Discuss] ...

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

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

随机推荐

  1. 读取Excel复杂的数据

    涉及到合并单元格的数据读取: package com.util; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util ...

  2. Nodejs WEB开发常用库和框架

    我在Nodejs的体系里也算泡了很久了,的确非常喜欢javascript和Nodejs. 在我看来,用nodejs做web开发有以下几个优点: Javascript作为一个语法异常简单的脚本语言,约束 ...

  3. linux top ps 命令

    http://javawind.net/p131 VIRT:virtual memory usage 虚拟内存1.进程“需要的”虚拟内存大小,包括进程使用的库.代码.数据等2.假如进程申请100m的内 ...

  4. Java解析XML的四种方法详解 - 转载

    XML现在已经成为一种通用的数据交换格式,平台的无关性使得很多场合都需要用到XML.本文将详细介绍用Java解析XML的四种方法 在做一般的XML数据交换过程中,我更乐意传递XML字符串,而不是格式化 ...

  5. Java Swing窗体小工具实例 - 原创

    Java Swing窗体小工具实例 1.本地webserice发布,代码如下: 1.1 JdkWebService.java package server; import java.net.InetA ...

  6. Longest Substring Without Repeating Characters,求没有重复字符的最长字串

    问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...

  7. scala学习手记8 - 自适应的默认做法

    scala有一些默认做法,会让代码更简洁.更易读写,下面列出了这样几个特性: 1. 支持脚本.scala支持脚本,因此无须将所有的代码都放到类里.如果脚本可以满足需求,就将代码放到一个脚本里,无须再创 ...

  8. 一些官方的github地址

    阿里巴巴开源github地址:https://github.com/alibaba 腾讯开源github地址:https://github.com/Tencent 奇虎360github地址:http ...

  9. 阿里云centOS7.4 nginx: [emerg] "server" directive is not allowed here in /etc/nginx/vhost/xxxxxx.conf:2

    里云centOS7.4配置多个站点遇到的问题nginx: [emerg] "server" directive is not allowed here in /etc/nginx/ ...

  10. python学习笔记(接口自动化框架 V2.0)

    这个是根据上次框架版本进行的优化 用python获取excel文件中测试用例数据 通过requets测试接口.并使用正则表达式验证响应信息内容 生成xml文件测试报告 版本更新内容: 1. 整理了Cr ...