bzoj 3277 & bzoj 3473,bzoj 2780 —— 广义后缀自动机
题目: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 —— 广义后缀自动机的更多相关文章
- BZOJ 3473: 字符串 (广义后缀自动机)
/* 广义后缀自动机, 每次加入维护 该right集合的set, 然后可以更新所有的parent,最终能够出现在k个串中right集合也就是set大小大于等于k的部分 这样的话就给了我们要跳的节点加了 ...
- BZOJ 4566 [Haoi2016]找相同字符 ——广义后缀自动机
建立广义后缀自动机. 然后统计子树中的siz,需要分开统计 然后对(l[i]-l[fa[i]])*siz[i][0]*siz[i][1]求和即可. #include <cstdio> #i ...
- BZOJ 2806 Luogu P4022 [CTSC2012]Cheat (广义后缀自动机、DP、二分、单调队列)
题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=2806 (luogu) https://www.luogu.org/pro ...
- bzoj 4566 [Haoi2016]找相同字符——广义后缀自动机
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4566 每个后缀结尾处 ct[ ] = 1 ,按拓扑序 dp 一下就能求出 right 集合的 ...
- 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 ...
- BZOJ 3277/3473 广义后缀自动机
说实话没啥难的. 建一棵广义后缀自动机,暴力自底向上更新即可. 时间复杂度非常玄学,但据说是可以过的. 要注意每个串中相同的子串的贡献是都要加进去的,开始因为这个被坑了好久 QAQ Code: #in ...
- BZOJ 3277 串 (广义后缀自动机)
3277: 串 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 309 Solved: 118 [Submit][Status][Discuss] De ...
- BZOJ 3473: 字符串 [广义后缀自动机]
3473: 字符串 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 354 Solved: 160[Submit][Status][Discuss] ...
- BZOJ.2780.[SPOJ8093]Sevenk Love Oimaster(广义后缀自动机)
题目链接 \(Description\) 给定n个模式串,多次询问一个串在多少个模式串中出现过.(字符集为26个小写字母) \(Solution\) 对每个询问串进行匹配最终会达到一个节点,我们需要得 ...
随机推荐
- 无网络环境用pip安装python类包
1.现在有网络的电脑安装相应的包 pip install django 2.安装完成后 打包 1)新建一个文件夹(package),用来存放包: 2)执行 pip list #查看安装的包 pip f ...
- style、 currentStyle、 runtimeStyle、getComputedStyle区别分析
1.obj.style只能获得内嵌样式(inline Style)就是写在Tag里面的,他访问不到那些链接的外部css和在head中用<style>声明的style. 所以必须认识到在那些 ...
- IE10下阿里旺旺无法快速登录解决办法
一直都是用Chrome浏览器的,如果已经登录了旺旺,打开淘宝登录的时候都是会有快速登录的,免得手工输入用户名密码了.不经意间用IE10打开淘宝,登录时发现无法使用快速登录,一番研究后发现,IE做了保护 ...
- MYSQL的基本语句——思维导图
如图 思维导图图片链接 http://www.edrawsoft.cn/viewer/public/s/a1718332616630 有道云笔记图片链接 http://note.youdao.com/ ...
- 深入Jetty源码之Servlet框架及实现(Servlet、Filter、Registration)
概述 Servlet是Server Applet的缩写,即在服务器端运行的小程序,而Servlet框架则是对HTTP服务器(Servlet Container)和用户小程序中间层的标准化和抽象.这一层 ...
- 简单Trace类实现
<C++沉思录>27章内容修改后所得: /************************************************************************/ ...
- Java内存分析1 - 从两个程序说起
这次看一些关于JVM内存分析的内容. 两个程序 程序一 首先来看两个程序,这里是程序一:JVMStackTest,看下代码: package com.zhyea.robin.jvm; public c ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- APP耗电量专项测试整理
Android: (使用batterystats) 方法: 手机自带的电量监控.GT 命令(5.0以上系统才可以): 1.下载historian.py脚本,下载地址:https://github.co ...
- iOS自动化探索(五)自动化测试框架pytest - Assert断言的使用
使用assert语句进行断言 pytest允许使用标准的python assert语法,用来校验expectation and value是否一致 代码演示: def func(): def test ...