bzoj 3277: 串 & bzoj 3473: 字符串【后缀自动机||后缀数组】
建一个广义后缀自动机(每加完一个串都返回root),在parent树上dpsum记录合法长度,打着时间戳往上跳,最后每个串在自动机上跑一变统计答案即可。
后缀数组理解起来可能方便一点,但是难写,就只说一下思路……把这些串加上特殊字符拼起来,然后按着sa扫,对每个位置二分长度,再左右端点(用height判断是否有k个)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=300005;
int n,k;
long long ans;
string s[N];
struct sam
{
int a[N][27],fa[N],len[N],v[N],c[N];
long long sum[N];
int p,q,np,nq,la,cnt;
sam()
{
la=++cnt;
}
void build(int c)
{
p=la;
np=la=++cnt;
len[np]=len[p]+1;
while(!a[p][c]&&p)
a[p][c]=np,p=fa[p];
if(!p)
fa[np]=1;
else
{
q=a[p][c];
if(len[q]==len[p]+1)
fa[np]=q;
else
{
nq=++cnt;len[nq]=len[p]+1;
memcpy(a[nq],a[q],sizeof(a[q]));
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
while(a[p][c]==q)
a[p][c]=nq,p=fa[p];
}
}
}
void clc(int x)
{
if(x==1||v[x])
return;
v[x]=1;
clc(fa[x]);
sum[x]+=sum[fa[x]];
}
}sam;
int main()
{
// freopen("string.in","r",stdin);
// freopen("string.out","w",stdout);
ios::sync_with_stdio(false);
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>s[i];
for(int j=0;j<s[i].length();j++)
sam.build(s[i][j]-'a');
sam.la=1;
}
for(int i=1;i<=n;i++)
{
int x=1,now=i;
for(int j=0;j<s[i].length();j++)
{
x=sam.a[x][s[i][j]-'a'];
int t=x;
while(t&&sam.v[t]!=now)
sam.v[t]=now,sam.c[t]++,t=sam.fa[t];
}
}
for(int i=1;i<=sam.cnt;i++)
sam.v[i]=0;
for(int i=1;i<=sam.cnt;i++)
sam.sum[i]=(sam.c[i]>=k)*(sam.len[i]-sam.len[sam.fa[i]]);
for(int i=1;i<=sam.cnt;i++)
sam.clc(i);
for(int i=1;i<=n;i++)
{
int x=1;ans=0;
for(int j=0;j<s[i].length();j++)
x=sam.a[x][s[i][j]-'a'],ans+=sam.sum[x];
cout<<ans<<endl;;
}
return 0;
}
bzoj 3277: 串 & bzoj 3473: 字符串【后缀自动机||后缀数组】的更多相关文章
- 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 串 & BZOJ 3473 字符串 (广义后缀自动机、时间复杂度分析、启发式合并、线段树合并、主席树)
标签那么长是因为做法太多了... 题目链接: (bzoj 3277) https://www.lydsy.com/JudgeOnline/problem.php?id=3277 (bzoj 3473) ...
- BZOJ 3277: 串/ BZOJ 3473: 字符串 ( 后缀数组 + RMQ + 二分 )
CF原题(http://codeforces.com/blog/entry/4849, 204E), CF的解法是O(Nlog^2N)的..记某个字符串以第i位开头的字符串对答案的贡献f(i), 那么 ...
- [模板] 后缀自动机&&后缀树
后缀自动机 后缀自动机是一种确定性有限状态自动机, 它可以接收字符串\(s\)的所有后缀. 构造, 性质 翻译自毛子俄罗斯神仙的博客, 讲的很好 后缀自动机详解 - DZYO的博客 - CSDN博客 ...
- poj 1743 Musical Theme 后缀自动机/后缀数组/后缀树
题目大意 直接用了hzwer的题意 题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题."主题&qu ...
- BZOJ 3277 串 (广义后缀自动机)
3277: 串 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 309 Solved: 118 [Submit][Status][Discuss] De ...
- BZOJ 3473: 字符串 (广义后缀自动机)
/* 广义后缀自动机, 每次加入维护 该right集合的set, 然后可以更新所有的parent,最终能够出现在k个串中right集合也就是set大小大于等于k的部分 这样的话就给了我们要跳的节点加了 ...
- BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机 后缀自动机 字符串
https://www.lydsy.com/JudgeOnline/problem.php?id=3926 广义后缀自动机是一种可以处理好多字符串的一种数据结构(不像后缀自动机只有处理一到两种的时候比 ...
- bzoj 3277 串 后缀树+子树不同数个数
题目大意 给定\(n\)个字符串和\(k\) 对于每个字符串,输出它有多少个子串至少是\(k\)个字符串的子串(包括自己) 分析 建出广义后缀自动机 至少是\(k\)个字符串的子串就是求子树内不同数个 ...
随机推荐
- Intel Naming Strategy--1
http://en.wikipedia.org/wiki/Mobile_Internet_device Computer sizes Classes of computers Larger S ...
- 使用Poi对EXCLE的导入导出
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
- poj2406--Power Strings(kmp:求循环串的次数)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 33163 Accepted: 13784 D ...
- Python——list切片
前文简单介绍了Python中的list和它常用的一些函数,知道list是一个有序的数据集合,那么我们如何获取list中的元素呢? Index: 与C语言中数组一样,list可以通过每个元素的index ...
- busybox 终端支持 ctrl-r
Busybox Settings ---> Busybox Library Tuning ---> [*] History saving [ ] Save history on shell ...
- HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA
状态压缩dp+spfa解斯坦纳树 枚举子树的形态 dp[i][j] = min(dp[i][j], dp[i][k]+dp[i][l]) 当中k和l是对j的一个划分 依照边进行松弛 dp[i][j] ...
- hibernate预编译SQL语句中的setParameter和setParameterList
使用预编译SQL语句和占位符參数(在jdbc中是?),可以避免由于使用字符串拼接sql语句带来的复杂性.我们先来简单的看下.使用预编译SQL语句的优点. 使用String sql = "se ...
- TreeSet实现Comparator接口的排序算法的分析
为了方便,用lambda表达式代替comparator接口 例子如下: public static void main(String[] args) { TreeSet<Integer> ...
- GPG key
Creating GPG Keys - Fedora Project Wiki https://fedoraproject.org/wiki/Creating_GPG_Keys
- BZOJ5379: Tree
BZOJ5379: Tree Description JudgeOnline/upload/201806/1.pdf 题解Here! 题目大意就是:1. 换根.2. 对$LCA(u,v)$的子树修改. ...