题目

给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串?

输入格式

第一行两个整数n,k。

接下来n行每行一个字符串。

输出格式

一行n个整数,第i个整数表示第i个字符串的答案。

输入样例

3 1

abc

a

ab

输出样例

6 1 3

提示

对于 100% 的数据,1<=n,k<=105,所有字符串总长不超过105,字符串只包含小写字母。

题解

我们先建一个广义后缀自动机

然后用每个原串在SAM上走,走到的节点就是parent树的叶子节点,将其沿parent边一直往上+1,表示这些节点表示的字符串+1【当然只能加一次,所以再开一个数组cur[]表示加过没有】

处理完后,我们就可以统计答案了

先拓扑排序,设f[]为该位置满足题意的字符串个数,显然如果一个位置累加的字符串>=k,那么该位置表示的至少为\(step[u] - step[pre[u]]\)

但还不完全,父亲的代表的字符串个数同样也符合该点

然后每个串再在SAM上走一遍统计答案即可

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 200005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
string s[maxn];
char ss[maxn];
int ch[maxn][26],pre[maxn],step[maxn],sz[maxn],cnt,last,n,k,cur[maxn];
LL f[maxn];
int ins(int x){
int p = last,np = ++cnt; step[np] = step[p] + 1; last = np;
while (p && !ch[p][x]) ch[p][x] = np,p = pre[p];
if (!p) pre[np] = 1;
else {
int q = ch[p][x];
if (step[q] == step[p] + 1) pre[np] = q;
else {
int nq = ++cnt; step[nq] = step[p] + 1;
for (int i = 0; i < 26; i++) ch[nq][i] = ch[q][i];
pre[nq] = pre[q]; pre[np] = pre[q] = nq;
while (ch[p][x] == q) ch[p][x] = nq,p = pre[p];
}
}
return np;
}
int b[maxn],a[maxn];
LL ans;
void tsort(){
REP(i,cnt) b[step[i]]++;
REP(i,cnt) b[i] += b[i - 1];
for (int i = cnt; i; i--) a[b[step[i]]--] = i;
}
int main(){
n = read(); k = read();
last = cnt = 1;
for (int i = 1; i <= n; i++){
last = 1;
scanf("%s",ss); s[i] = string(ss);
int len = strlen(ss);
for (int i = 0; i < len; i++) ins(ss[i] - 'a');
}
int u;
for (int i = 1; i <= n; i++){
u = 1;
for (int j = 0; j < s[i].length(); j++){
u = ch[u][s[i][j] - 'a'];
for (int p = u; p && cur[p] != i; p = pre[p])
sz[p]++,cur[p] = i;
}
}
tsort();
sz[1] = 0;
for (int i = 1; i <= cnt; i++)
u = a[i],f[u] = f[pre[u]] + (sz[u] >= k ? step[u] - step[pre[u]] : 0);
for (int i = 1; i <= n; i++){
ans = 0; u = 1;
for (int j = 0; j < s[i].length(); j++){
u = ch[u][s[i][j] - 'a'];
ans += f[u];
}
printf("%lld ",ans);
}
return 0;
}

BZOJ3473 字符串 【广义后缀自动机】的更多相关文章

  1. BZOJ3473 字符串 广义后缀自动机

    今天主攻了下SAM 好多东西以前都没理解到 对于这道题 我们建一个自动机存所有串 每个穿last从1开始 对于自动机上每个点额外记一个cnt 表示能匹配到这个点的不同串个数 建完对每个串在自动机上匹配 ...

  2. 【bzoj3277/bzoj3473】串/字符串 广义后缀自动机

    题目描述 字符串是oi界常考的问题.现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串(注意包括本身). 输入 第一行两个整数n,k.接下来n行每行一个 ...

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

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

  4. BZOJ 3277 串 & BZOJ 3473 字符串 (广义后缀自动机、时间复杂度分析、启发式合并、线段树合并、主席树)

    标签那么长是因为做法太多了... 题目链接: (bzoj 3277) https://www.lydsy.com/JudgeOnline/problem.php?id=3277 (bzoj 3473) ...

  5. 2018.12.22 bzoj3473: 字符串(后缀自动机+启发式合并)

    传送门 调代码调的我怀疑人生. 启发式合并用迭代写怎么都跑不过(雾 换成了dfsdfsdfs版本的终于过了233. 题意简述:求给出nnn个字串,对于每个给定的字串求出其有多少个字串在至少kkk个剩下 ...

  6. BZOJ 3473 字符串 ——广义后缀自动机

    这题就比较有趣了. 首先匹配一遍,然后统计子树叶子节点中包含大于等于k的节点个数(HH的项链) 然后就可以搞了. 关于合法的情况数,显然是l[i]-l[fa[i]],然后向下下传即可(YY一下). # ...

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

  8. BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机 后缀自动机 字符串

    https://www.lydsy.com/JudgeOnline/problem.php?id=3926 广义后缀自动机是一种可以处理好多字符串的一种数据结构(不像后缀自动机只有处理一到两种的时候比 ...

  9. [bzoj3277==bzoj3473]出现k次子串计数——广义后缀自动机+STL

    Brief Description 给定n个字符串,对于每个字符串,您需要求出在所有字符串中出现次数大于等于k次的子串个数. Algorithm Design 先建立一个广义后缀自动机,什么是广义后缀 ...

随机推荐

  1. Jenkins怎么启动和停止服务

    笔者没有把Jenkins配置到tomcat中,每次都是用命令行来启动Jenkins.但是遇到一个问题:Jenkins一直是开着的,想关闭也关闭不了.百度了一些资料,均不靠谱(必须吐槽一下百度).于是进 ...

  2. 为 Azure 应用服务配置连续部署工作流

    本快速入门介绍了如何将应用服务 GitHub 集成以实现连续部署工作流.在本教程中完成的所有操作均符合1元试用条件. 本快速入门介绍了如何将应用服务 GitHub 集成以实现连续部署工作流.在本教程中 ...

  3. 忘记Centos7.2下root用户密码后的处理方式

    1)重启系统 重新启动系统后并按f2键,进入如下的界面,再按e键. 2)修改启动内核代码 在代码的linux16行中,将ro rhgb的ro修改为rw init=/sysroot/bin/sh. 3) ...

  4. (WWWWWWWWWW)codevs 3305 水果姐逛水果街Ⅱ

    写这么长了不A有点舍不得.. 想A又调不出来.. 于是乎就存一下.. 屠龙宝刀点击就送 #include <cstdio> #include <vector> #define ...

  5. block总结我的

    1) struct Block_descriptor { unsigned long int reserved; unsigned long int size; void (*copy)(void * ...

  6. 用NSCoding协议完成“编码/解码”操作-Object-C

    Archiving Objective-C Objects with NSCoding For the seasoned Cocoa developer, this is a piece of cak ...

  7. ace editor 使用教程

    <!DOCTYPE html><html> <head> <title>Demo of ACE Editor</title> <!-- ...

  8. JS 、JQ 获取宽高总结 & JS中getBoundingClientRect的作用及兼容方案

    1.getBoundingClientRect的作用 getBoundingClientRect用于获取某个html元素相对于视窗的位置集合.   执行 object.getBoundingClien ...

  9. 火狐浏览器返回不加载JS

    火狐浏览器 go(-1),返回后不加载JS,谷歌会加载. 总结: Firefox和Safari在back时不会触发load, ready事件! 解决方法: $(window).unload(funct ...

  10. 爬虫4_python2

    import urllib2 response = urllib2.urlopen("https://www.baidu.com") print response.read() 构 ...