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\) 对每个询问串进行匹配最终会达到一个节点,我们需要得 ...
随机推荐
- bootstrap正则表达式验证手机 座机 邮箱
$('#CusForm').bootstrapValidator({ fields : { //验证手机 'customer.mobile' : { //input中的name 值 validator ...
- cn_03_r2_enterprise_sp2_x86_vl_X13_46432
1. 使用的 ISO为:cn_win_srv_2003_r2_enterprise_with_sp2_vl_cd1_X13-46432.iso 2.序列号 用的序列号是“DF74D-TWR86-D3F ...
- LeetCode第[66]题(Java):Plus One
题目:数组加一 难度:Easy 题目内容: Given a non-empty array of digits representing a non-negative integer, plus ...
- python 爬虫002-http与urllib2
urllib2 GET https://www.oschina.net/home/login #!/usr/bin/env python # -*- coding: utf-8 -*- import ...
- ubuntu 编译 vim+lua
mac $ brew install vim --with-cscope --with-lua --override-system-vim 安装spf13-vim见下面linux部分. 如果安装mac ...
- uva 11825 巧妙地子集枚举方法
https://vjudge.net/problem/UVA-11825 题目大意,有n台服务器,有n种服务,每台服务器都运行着所有的服务,一台服务器可以被攻击一次其中的一种服务,当你选择攻击某台服务 ...
- Could not publish server configuration for Tomcat v6.0 Server at localhost.错误问题解决
经常在使用tomcat服务器的时候 总会发生一些莫名其妙的错误. 就像下面这个错误: 在配置文件中存在多个/MyWeb的配置,导致不能发布服务. 错误信息: Could not publish ser ...
- Centos6.8 JDK配置
记录一下在这个服务器配置的过程 ssh root@IP Password --------------------------------------------------------------- ...
- DRF 返回数据的封装,和分页
DRF 返回数据的封装,和分页 1 返回值的 封装 自定义一个类,初始化基本的返回数据信息 class BaseResponse(object): """ 初始化基本的返 ...
- 4.CRT远程连接的使用
目录: 1.为什么需要远程连接? 2.一般的远程连接工具有哪些? 3.远程连接的原理? 4.远程连接的软件的功能和使用相关技巧? 1.为什么选择远程连接? 因为在实际工作中,机房一般都不可能在办公室, ...