[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 ...
随机推荐
- ubuntu 安装wine
笔记 1.安装源 sudo add-apt-repository ppa:wine/wine-builds sudo apt-get update 2.安装wine sudo apt-get inst ...
- spring boot mysql和mybatis
1 选择mysql驱动 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connecto ...
- swift 使用运行时进行属性关联
1.用OC思想写swift代码真得很爽,swift需要的OC基本上都有,只不过略微改变了一下,例如以前的Foundation库前缀NS全部去掉了,等等...思想其实都一样,不过swift确实非常精简, ...
- Android系统移植与调试之------->如何修改Android设备添加重启、飞行模式、静音模式等功能(二)
今天要说的是为Android设备添加重启.飞行模式.静音模式按钮,客户需求中需要添加这项功能,在长按电源键弹出的菜单中没有这些选项,谨以此文记录自己添加这个功能的过程. 首先找到长按电源键弹出的对话框 ...
- 我的Android进阶之旅------>Android中AsyncTask源码分析
在我的<我的Android进阶之旅------>android异步加载图片显示,并且对图片进行缓存实例>文章中,先后使用了Handler和AsyncTask两种方式实现异步任务机制. ...
- lzugis—搭建属于自己的小型的版本号控制SVN
版权声明:本文为LZUGIS原创文章,未经同意不得转载. https://blog.csdn.net/GISShiXiSheng/article/details/28643575 对于不了解SVN的同 ...
- action extension添加图标
最近在做ios的action extension,这里记录一下添加图标的方法. 在Action Extension的target里面的Build Settings,里面的Asset Catalog C ...
- jQuery悬浮焦点图宽屏
在线演示 本地下载
- awk过滤磁盘使用率
过滤出大于某个值的磁盘使用率: #!/bin/bash diskspace=`df -Ph`IFS="\n" for line in $diskspace;do ec ...
- VNC服务安装、配置与使用
原帖地址: http://blog.itpub.net/519536/viewspace-607549/ 该文档配置环境是RHEL,不同系统可能会有差别,本人测试过centos,ubuntu 1.确认 ...