BZOJ3277——串
0、题意:给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串(注意包括本身)。
1、分析:这个题我问了吴大爷做法
首先建立后缀自动机,然后利用离线搞出每一个节点在多少个串中,这个用树状数组统计一下就好,和BZOJ2780一样,然后如果这个节点在不少于x个串中,我们把这个点的value赋值成这个节点父亲边的字符个数,否则为0。随后我们预处理一下从根到每个节点路径上的权值和。于是每个字符串的答案等于所有这个字符串的后缀节点的从根到该节点的权值和。
然后这个题就解决了
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 200010
#define LL long long
inline int read(){
char ch = getchar(); int x = 0, f = 1;
while(ch < '0' || ch > '9'){
if(ch == '-') f = -1;
ch = getchar();
}
while('0' <= ch && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
struct node{
int tranc[27], fa, len;
} a[202010];
int cnt, tail;
char s[202010];
int p, nq, q, np;
int in[M], out[M], seg[M], num;
int C[M], v[M];
int lastins[M];
int sum[M];
struct gragh{
int head[M], to[M], Next[M], tot;
inline void init(){
memset(head, -1, sizeof(head));
tot = 0;
}
inline void add(int u, int v){
++ tot;
Next[tot] = head[u];
head[u] = tot;
to[tot] = v;
}
} w, G, sam;
struct Query{
int id, l, r;
inline bool operator < (const Query& rhs) const{
return r < rhs.r;
}
} S[M];
inline void insert(int k, int op){
p = tail;
if(a[p].tranc[k] != 0){
q = a[p].tranc[k];
if(a[q].len == a[p].len + 1) tail = q;
else{
a[nq = ++ cnt] = a[q];
a[q].fa = nq;
a[nq].len = a[p].len + 1;
for(; a[p].tranc[k] == q; p = a[p].fa) a[p].tranc[k] = nq;
tail = nq;
}
}
else{
np = ++ cnt;
a[np].len = a[tail].len + 1;
for(p = tail; p && !a[p].tranc[k]; p = a[p].fa) a[p].tranc[k] = np;
if(!p) a[np].fa = 1;
else{
q = a[p].tranc[k];
if(a[q].len == a[p].len + 1) a[np].fa = q;
else{
a[nq = ++ cnt] = a[q];
a[q].fa = a[np].fa = nq;
a[nq].len = a[p].len + 1;
for(; a[p].tranc[k] == q; p = a[p].fa) a[p].tranc[k] = nq;
}
}
tail = np;
}
w.add(tail, op);
sam.add(op, tail);
}
inline void dfs(int x){
in[x] = ++ num; seg[num] = x;
for(int i = G.head[x]; i != -1; i = G.Next[i]){
dfs(G.to[i]);
}
out[x] = num;
}
inline void change(int x, int d){
for(; x <= cnt; x += (x & -x)) C[x] += d;
}
inline int ask(int x){
int ret = 0;
for(; x > 0; x -= (x & -x)) ret += C[x];
return ret;
}
inline void dfs2(int x){
v[x] += v[a[x].fa];
for(int i = G.head[x]; i != -1; i = G.Next[i]) dfs2(G.to[i]);
}
int main(){
int n = read(), lim = read();
cnt = 1;
G.init(); w.init(); sam.init();
for(int i = 1; i <= n; i ++){
scanf("%s", s + 1);
tail = 1;
int len = strlen(s + 1);
for(int j = 1; j <= len; j ++){
insert(s[j] - 'a' + 1, i);
}
}
for(int i = 1; i <= cnt; i ++){
if(a[i].fa != 0) G.add(a[i].fa, i);
}
dfs(1);
for(int i = 1; i <= cnt; i ++) S[i] = (Query){i, in[i], out[i]};
sort(S + 1, S + cnt + 1);
int k = 1;
for(int i = 1; i <= cnt; i ++){
for(int j = w.head[seg[i]]; j != -1; j = w.Next[j]){
if(lastins[w.to[j]]) change(lastins[w.to[j]], -1);
change(lastins[w.to[j]] = i, 1);
}
while(S[k].r == i) sum[S[k].id] = ask(S[k].r) - ask(S[k].l - 1), k ++;
}
for(int i = 1; i <= cnt; i ++){
if(sum[i] >= lim) v[i] = a[i].len - a[a[i].fa].len;
else v[i] = 0;
}
dfs2(1);
LL ans = 0;
for(int i = 1; i <= n; i ++){
if(i > 1) printf(" ");
ans = 0;
for(int j = sam.head[i]; j != -1; j = sam.Next[j]) ans += v[sam.to[j]];
printf("%lld", ans);
}
return 0;
}
BZOJ3277——串的更多相关文章
- BZOJ3473&&BZOJ3277串
BZOJ3473&&BZOJ3277串 题面 自己找去 HINT 对于所有串建立一个广义后缀自动机,对于每一个节点开一个set表示这个节点接受的子串在哪些串里出现过,然后在parent ...
- BZOJ3277 串(后缀数组+二分答案+主席树)
因为不会SAM,考虑SA.将所有串连起来并加分隔符,每次考虑计算以某个位置开始的子串有多少个合法. 对此首先二分答案,找到名次数组上的一个区间,那么只需要统计有多少个所给串在该区间内出现就可以了.这是 ...
- bzoj3277 串 (后缀数组+二分答案+ST表)
常见操作:先把所有串都连到一起,但中间加上一个特殊的符号(不能在原串中/出现过)作为分割 由于全部的子串就等于所有后缀的所有前缀,那我们对于每一个后缀,去求一个最长的前缀,来满足这个前缀在至少K个原串 ...
- bzoj3473: 字符串 && bzoj3277串
3473: 字符串 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 121 Solved: 53[Submit][Status][Discuss] D ...
- 【文文殿下】[BZOJ3277] 串
Description 字符串是oi界常考的问题.现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中 至少k个字符串的子串(注意包括本身) Input 第一行两个整数n,k ...
- BZOJ3277 串 【广义后缀自动机】
Description 字符串是oi界常考的问题.现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中 至少k个字符串的子串(注意包括本身). Input 第一行两个整数n, ...
- bzoj3473字符串&bzoj3277串
题意:给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串.注意本质相同的子串多次出现算多次,如1 1 aaa这组数据答案为6,贡献1WA.代码里有些部分是为了 ...
- BZOJ3277: 串(广义后缀自动机)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1196 Solved: 478[Submit][Status][Discuss] Descripti ...
- BZOJ3277 串(后缀自动机)
对多串建立SAM的一种方法是加分隔符.于是加完分隔符建出SAM. 考虑统计出每个节点被多少个串包含.让每个串各自在SAM上跑,跑到一个节点就标记(显然一定会完全匹配该节点,因为是对包含其的串建的SAM ...
随机推荐
- django1.9 + uwsgi +nginx1.9 部署(centos6.6)
django1.9 + uwsgi +nginx1.9 部署 官方介绍 https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_ngin ...
- excel2013添加坐标轴名称label
图画好了,x.y轴没有名称,怎么办那 点击左上角有个---添加图标元素----里面有轴标题应该就是
- mysql修改引擎
1 查看系统支持的存储引擎 show engines; 2 查看表使用的存储引擎 两种方法: a.show table status from db_name where name='table_na ...
- matlab连接sql数据库
最近项目还涉及到matlab连接数据库,下面我就记录如何进行配置使得matlab能够连接sql数据库.由于最近工程做的多一些,所以分享的都在工程配置上,当初为了这些配置可是反复卸载与重装,算法其实也有 ...
- maven学习讲解
参考链接:http://www.cnblogs.com/bigtall/archive/2011/03/23/1993253.html 1.前言 Maven,发音是[`meivin],"专家 ...
- win7安装virtualbox
1.下载软件 VirtualBox-4.3.24-98716-Win.1425444683.exe 2.修改安装路径 3.确定选择下一步 4.下一步 5.yes 6.安装 7.安装完成 到此win7 ...
- 仿照jquery封装一个自己的js库(二)
本篇为完结篇.主要讲述如何造出轮子的高级特性. 一. css方法的高级操作 先看本文第一部分所讲的dQuery css方法 //css方法 dQuery.prototype.css=function( ...
- C#--之文件操作
1.从文本文件中读取一行文本 StreamReader sr = new StreamReader("C:\\1.txt"); string readline = sr.ReadL ...
- Python 系列:1 - Tuples and Sequences
5.3 Tuples and Sequences We saw that lists and strings have many common properties, e.g., indexing a ...
- 轻量级router[类似laravel router]
github地址:https://github.com/NoahBuscher/Macaw/blob/master/Macaw.php 代码加上一些注释,方便以后再看. <?php namesp ...