[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 ...
随机推荐
- centos7.0 安装redis 3.2.9
wget http://download.redis.io/releases/redis-3.2.9.tar.gz tar xzf redis-3.2.9.tar.gz cd redis-3.2.9 ...
- CSS中设置div垂直居中
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...
- 【BZOJ2724】[Violet 6]蒲公英 分块+二分
[BZOJ2724][Violet 6]蒲公英 Description Input 修正一下 l = (l_0 + x - 1) mod n + 1, r = (r_0 + x - 1) mod n ...
- 成功扩展live555支持ipv6,同时支持RTSPServer & RTSPClient
live555对ipv6的扩展 从live555的官网看live555的发展历史,实在是历史悠久,保守估计已经发展了至少16年以上了,同时,这也导致了live555在很多架构和考虑上面不能满足现代化的 ...
- 【HTML5开发系列】表单元素
<form> 创建一个HTML表单 属性: action 表示提交表单时浏览器应该把用户填写的数据发送到什么地方 method 用来指定表单数据发送到服务器的方式.允许值有get和post ...
- 通过GPRS将GPS数据上传到OneNet流程
AT OK AT+CGCLASS="B" OK AT+CGDCONT=1,"IP","CMNET" OK AT+CGATT=1 OK AT+ ...
- rails 运维常用命令
创建生产环境数据库并执行迁移 $ RAILS_ENV=production rake db:create$ RAILS_ENV=production rake db:migrate RAILS_ENV ...
- 【leetcode刷题笔记】Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【五】MongoDB管理之生产环境说明
下面详细说明影响mongodb的系统配置,尤其在生产环境上. 1.生产环境推荐的平台 Amazon Linux Debian 7.1 Red Hat / CentOS 6.2+ SLES 11+ Ub ...
- Delphi 的进制转换
1.10进制转16进制 intTohex(10,4); //第一个参数为要转换的数据,第二个参数为要转换后的16进制位数:得到:000A; 2. 16进制转10进制 strToInt('$'+'64 ...