BZOJ 3473
思路:
CF原题
ZYF有题解
O(nlog^2n)
//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
const int N=;
int n,m,q,cntA[N],cntB[N],A[N],B[N],rk[N],ht[N],sa[N],tsa[N],f[N][];
int from[N],cnt[N],rec[N],tl[N],ans[N];
char ch[N],s[N];
void SA(){
for(int i=;i<=n;i++)cntA[s[i]]++;
for(int i=;i<=;i++)cntA[i]+=cntA[i-];
for(int i=n;i;i--)sa[cntA[s[i]]--]=i;
rk[sa[]]=;
for(int i=;i<=n;i++)rk[sa[i]]=rk[sa[i-]]+(s[sa[i]]!=s[sa[i-]]);
for(int l=;rk[sa[n]]<n;l<<=){
memset(cntA,,sizeof(cntA));
memset(cntB,,sizeof(cntB));
for(int i=;i<=n;i++)cntA[A[i]=rk[i]]++,cntB[B[i]=(i+l<=n?rk[i+]:)]++;
for(int i=;i<=n;i++)cntA[i]+=cntA[i-],cntB[i]+=cntB[i-];
for(int i=n;i;i--)tsa[cntB[B[i]]--]=i;
for(int i=n;i;i--)sa[cntA[A[tsa[i]]]--]=tsa[i];
rk[sa[]]=;
for(int i=;i<=n;i++)rk[sa[i]]=rk[sa[i-]]+(A[sa[i]]!=A[sa[i-]]||B[sa[i]]!=B[sa[i-]]);
}
for(int i=,j=;i<=n;i++){
j=j?j-:;
while(s[i+j]==s[sa[rk[i]-]+j])j++;
ht[rk[i]]=j;
}
for(int i=;i<=n;i++)f[i][]=ht[i];
for(int j=;j<=;j++)
for(int i=;i<=n;i++)
f[i][j]=min(f[i][j-],f[i+(<<(j-))][j-]);
}
bool check(int pos,int len){
int l=pos,r=pos;
for(int j=;~j;j--){
if(l+>=(<<j)&&f[l+-(<<j)][j]>=len)l-=(<<j);
if(f[r+][j]>=len)r+=(<<j);
}return rec[r]>=l;
}
int main(){
scanf("%d%d",&m,&q);
for(int i=;i<=m;i++){
scanf("%s",ch);
int t=strlen(ch);
s[n++]=' ';
for(int j=;j<t;j++)from[n]=i,s[n++]=ch[j];
tl[i]=n;
}n--,s[]=;SA();
int t=,k=;
for(int i=;i<=n;i++)if(from[sa[i]]){
if(!cnt[from[sa[i]]])k++;
cnt[from[sa[i]]]++;
if(k>=q){
for(;k-(cnt[from[sa[t]]]==)>=q;k-=(cnt[from[sa[t]]]==),--cnt[from[sa[t++]]]);
rec[i]=t;
}
}
for(int i=;i<=n;i++)if(from[sa[i]]){
int l=,r=tl[from[sa[i]]]-sa[i],dt=;
while(l<=r){
int mid=(l+r)>>;
if(check(i,mid))dt=mid,l=mid+;
else r=mid-;
}ans[from[sa[i]]]+=dt;
}
for(int i=;i<=m;i++)printf("%d ",ans[i]);
}
BZOJ 3473的更多相关文章
- 【BZOJ 3473】 字符串 (后缀数组+RMQ+二分 | 广义SAM)
3473: 字符串 Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串 ...
- BZOJ 3277 串 & BZOJ 3473 字符串 (广义后缀自动机、时间复杂度分析、启发式合并、线段树合并、主席树)
标签那么长是因为做法太多了... 题目链接: (bzoj 3277) https://www.lydsy.com/JudgeOnline/problem.php?id=3277 (bzoj 3473) ...
- bzoj 3473 字符串 - 后缀数组 - 树状数组
题目传送门 传送门 题目大意 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串 先用奇怪的字符把所有字符串连接起来. 建后缀树,数每个节点的子树内包含多少属 ...
- BZOJ 3473: 字符串 [广义后缀自动机]
3473: 字符串 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 354 Solved: 160[Submit][Status][Discuss] ...
- 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 3473 后缀自动机多字符串的子串处理方法
后缀自动机处理多字符串字串相关问题. 首先,和后缀数组一样,用分割符连接各字符串,然后建一个后缀自动机. 我们定义一个节点代表的字符串为它原本代表的所有串去除包含分割符后的串.每个节点代表的字符串的数 ...
- bzoj 3277 & bzoj 3473,bzoj 2780 —— 广义后缀自动机
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3277 https://www.lydsy.com/JudgeOnline/problem.p ...
- BZOJ 3277: 串/ BZOJ 3473: 字符串 ( 后缀数组 + RMQ + 二分 )
CF原题(http://codeforces.com/blog/entry/4849, 204E), CF的解法是O(Nlog^2N)的..记某个字符串以第i位开头的字符串对答案的贡献f(i), 那么 ...
- BZOJ 3473 字符串
思路 广义SAM的题目,先全部插入,然后每个字符串在SAM上匹配,如果发现当前sz小于k(就是前缀不满足条件),就跳fail(找前缀的后缀,就是找子串)到满足条件为止,然后一个满足条件的节点,它的所有 ...
- BZOJ 3473: 字符串 (广义后缀自动机)
/* 广义后缀自动机, 每次加入维护 该right集合的set, 然后可以更新所有的parent,最终能够出现在k个串中right集合也就是set大小大于等于k的部分 这样的话就给了我们要跳的节点加了 ...
随机推荐
- list数组排序---stream
import java.util.*;import java.util.stream.Collector;import java.util.stream.Collectors; public clas ...
- 洛谷 2866 [USACO06NOV]糟糕的一天Bad Hair Day
[题意概述] 给出一个长度为n的序列a,求有多少对[i,j]满足i<j且a[i]>max(a[i+1],a[i+2],...,a[j]). [题解] 单调栈. 倒着处理序列的元素,维护一个 ...
- 初学hash
hash定义: Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种 ...
- AOP基础
[Why AOP ?] 1.代码混乱:越来越多的非业务需求(日志和验证等)加入后,原有的业务方法急剧膨胀.每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点. 2.代码分散:以日志需求为例,知识为了 ...
- codevs1018 单词接龙
题目描述 Description 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次), ...
- 五语言学习系列 C,C++,Objective-C,Java,C# (一)历史
C:由AT&T贝尔实验室的Dennis Ritchie于1972年创建的,是专为开发者设计的语言. C++:在C基础上,1983年又由贝尔实验室的Bjarne Strou-strup推出了C+ ...
- ACM 数学
欧几里得辗转相除法求最大公约数 int gcd(int a,int b) { ) return a; else return gcd(b,a%b); } 求组合数 int C(int n ,int m ...
- cogs——1298. 通讯问题
1298. 通讯问题 ★★ 输入文件:jdltt.in 输出文件:jdltt.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 一个篮球队有n个篮球队员,每个队员 ...
- LeetCode240:Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- C++对象内存分布(3) - 菱形继承(virtual)
1.前言 本篇文章的全部代码样例.假设是windows上编译执行.则使用的是visual studio 2013.假设是RHEL6.5平台(linux kernal: 2.6.32-431.el6.i ...