[Codeforces 204E] Little Elephant and Strings
[题目链接]
https://codeforces.com/contest/204/problem/E
[算法]
首先构建广义后缀自动机
对于自动机上的每个节点 , 维护一棵平衡树存储所有它所匹配的字符串编号
可以通过启发式合并得到
计算答案时 , 我们枚举每个右端点 , 当当前集合大小 < K时 , 不断走向link节点
时间复杂度 : O(NlogN)
[代码]
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 2e5 + ;
const int ALPHA = ;
#define rint register int int n , k;
string st[N]; struct Suffix_Automaton
{
int sz , last;
int father[N] , depth[N] , child[N][ALPHA] , cnt[N];
set< int > s[N];
vector< int > a[N];
Suffix_Automaton()
{
sz = ;
last = ;
}
inline int new_node(int dep)
{
depth[++sz] = dep;
father[sz] = ;
memset(child[sz] , , sizeof(child[sz]));
return sz;
}
inline void extend(int ch , int col)
{
int np = child[last][ch];
if (np)
{
if (depth[np] == depth[last] + )
{
s[np].insert(col);
last = np;
return;
} else
{
int nq = new_node(depth[last] + );
father[nq] = father[np];
father[np] = nq;
memcpy(child[nq] , child[np] , sizeof(child[nq]));
for (int p = last; child[p][ch] == np; p = father[p])
child[p][ch] = nq;
s[nq].insert(col);
last = nq;
return;
}
} else
{
np = new_node(depth[last] + );
int p = last;
for (; child[p][ch] == ; p = father[p])
child[p][ch] = np;
if (child[p][ch] == np)
{
father[np] = ;
s[np].insert(col);
last = np;
return;
} else
{
int q = child[p][ch];
if (depth[q] == depth[p] + )
{
father[np] = q;
s[np].insert(col);
last = np;
return;
} else
{
int nq = new_node(depth[p] + );
father[nq] = father[q];
father[np] = father[q] = nq;
memcpy(child[nq] , child[q] , sizeof(child[nq]));
for (; child[p][ch] == q; p = father[p])
child[p][ch] = nq;
s[np].insert(col);
last = np;
}
}
}
}
inline void insert(string s , int col)
{
last = ;
for (rint i = ; i < (int)s.size(); ++i)
extend(s[i] - 'a' , col);
}
inline void dfs(int u)
{
for (unsigned i = ; i < a[u].size(); ++i)
{
int v = a[u][i];
dfs(v);
if ((int)s[u].size() < (int)s[v].size())
swap(s[u] , s[v]);
for (set< int > :: iterator it = s[v].begin(); it != s[v].end(); ++it)
s[u].insert(*it);
}
cnt[u] = (int)s[u].size();
}
inline void work()
{
for (rint i = ; i <= sz; ++i)
a[father[i]].push_back(i);
dfs();
}
} SAM; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} int main()
{ read(n); read(k);
for (rint i = ; i <= n; ++i)
{
cin >> st[i];
SAM.insert(st[i] , i);
}
SAM.work();
for (rint i = ; i <= n; ++i)
{
int now = ;
ll ans = ;
for (rint j = ; j < st[i].size(); ++j)
{
now = SAM.child[now][st[i][j] - 'a'];
while (now != && SAM.cnt[now] < k) now = SAM.father[now];
ans += (ll)SAM.depth[now];
}
printf("%lld " , ans);
}
printf("\n"); return ; }
[Codeforces 204E] Little Elephant and Strings的更多相关文章
- codeforces 204E. Little Elephant and Strings(广义后缀自动机,Parent树)
传送门在这里. 大意: 给一堆字符串,询问每个字符串有多少子串在所有字符串中出现K次以上. 解题思路: 这种子串问题一定要见后缀自动机Parent树Dfs序统计出现次数都是套路了吧. 这道题统计子串个 ...
- 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...
- CodeForces - 204C Little Elephant and Furik and Rubik
CodeForces - 204C Little Elephant and Furik and Rubik 个人感觉是很好的一道题 这道题乍一看我们无从下手,那我们就先想想怎么打暴力 暴力还不简单?枚 ...
- CodeForces-204E:Little Elephant and Strings (广义后缀自动机求出现次数)
The Little Elephant loves strings very much. He has an array a from n strings, consisting of lowerca ...
- Codeforces D. Little Elephant and Interval(思维找规律数位dp)
题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...
- 【Codeforces 204E】Little Elephant and Strings
Codeforces 204 E 题意:给\(n\)个串,求对于每一个串在至少\(k\)个串中出现的它的子串\(S_{l..r}\)有多少个. 思路:后缀自动机上\(dp\)... 我们首先构造出这\ ...
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
题意:有n个串,询问每个串有多少子串在n个串中出现了至少k次. 题解:sam,每个节点开一个set维护该节点的字符串有哪几个串,启发式合并set,然后在sam上走一遍该串,对于每个可行的串,所有的fa ...
- CodeForces 259A Little Elephant and Chess
Little Elephant and Chess Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- Educational Codeforces Round 12 C. Simple Strings 贪心
C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...
随机推荐
- Apache启动失败(Windows 无法在本地计算机启动Apache2.2)
该问题产生的原因有很多,这里就说说我遇到的问题: 这个问题是突然遇到的,启动的时候连日志都没有产生,最后是通过window的日志中发现问题所在的,如图所示: 发现是目录错了.其他问题也可以通过该日志找 ...
- sharding-jdbc从入门到出门(03)
经过端午节这2天对 sharding-jdbc一直怀揣成梦想的去学习,还是有一些没有解决的问题: 上一张图:
- Android笔记之OnLongClickListener
OnLongClickListener中的回调函数boolean onLongClick(View v),其返回值的官方释义如下 如果这个回调消耗了长点击,则返回true,否则返回false. 即使翻 ...
- Js拼接html并给onclick传多个参数
return '<a id="" class="ace_button" href="#" onclick="showItem ...
- PostgreSQL 里面的 BIGSERIAL
@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id; CREATE TABLE article( id BIGS ...
- Java中的String,StringBuilder,StringBuffer三者的区别(转发:https://www.cnblogs.com/su-feng/p/6659064.html)
最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及StringBuffer这三个类之间有什么区别呢,自己从网上搜索了一些资料,有所了解了之后在这里整理一下, ...
- 计算机网络协议层次(转发:http://blog.csdn.net/gavin_john/article/details/53186570)
计算机网络学习的核心内容就是网络协议的学习.网络协议是为计算机网络中进行数据交换而建立的规则.标准或者说是约定的集合.计算机网络协议同我们的语言一样,多种多样. 为了给网络协议的设计提供一个结构,网络 ...
- vMware存储:SAN配置基础
VMware存储不仅仅是将LUN映射给物理服务器这么简单.VMware vSphere允许系统管理员在一个物理机上创建多个虚拟机. 潜在的hypervisor和vSphere ESXi,能够使gues ...
- 基于卡方的独立性检验原理及R语言实现
在读到<R语言实战>(第二版)P143页有关卡方独立性检验所记 假设检验 假设检验(Test of Hypothesis)又称为显著性检验(Test of Ststistical Sign ...
- docker swarm部署spring cloud服务
一.准备docker swarm的集群环境 ip 是否主节点 192.168.91.13 是 192.168.91.43 否 二.准备微服务 ①eureka服务 application.y ...