SPOJ 8222 NSUBSTR 后缀自动机
2016-04-15 20:48
399 查看
求长度为i的出现次数最多的子串的次数。
即找出min(s)≤i≤max(s)的状态i,使其Right集合元素个数最多。。
首先可以计算出每个状态的Right集合大小,即Parent树的子树的叶子节点数。
一直internal error也是坑。。
手动内联后就神奇地AC了?!
2
2
1
1
即找出min(s)≤i≤max(s)的状态i,使其Right集合元素个数最多。。
首先可以计算出每个状态的Right集合大小,即Parent树的子树的叶子节点数。
一直internal error也是坑。。
手动内联后就神奇地AC了?!
#include <cstring> #include <cstdio> #include <algorithm> using namespace std; #define FOR(i,j,k) for(i=j;i<=k;++i) const int rt = 1, N = 500005; int last = 1, cnt = 1, len = 0; int ch [26], fa , ma , v , b , dp , bucket ; char str ; void add(char c) { int np = ++cnt, p = last, q, nq; last = np; ma[np] = ++len; v[np] = 1; while (p && !ch[p][c]) ch[p][c] = np, p = fa[p]; if (!p) fa[np] = rt; else { q = ch[p][c]; if (ma[q] == ma[p] + 1) fa[np] = q; else { nq = ++cnt; memcpy(ch[nq], ch[q], sizeof ch[q]); ma[nq] = ma[p] + 1; fa[nq] = fa[q]; fa[q] = fa[np] = nq; v[nq] = 0; while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p]; } } } int main() { int i, n; scanf("%s", str + 1); n = strlen(str + 1); FOR(i,1,n) add(str[i] - 'a'); FOR(i,1,cnt) ++bucket[ma[i]]; FOR(i,1,n) bucket[i] += bucket[i - 1]; for(i=cnt;i;--i) b[bucket[ma[i]]--] = i; for(i=cnt;i;--i) v[fa[b[i]]] += v[b[i]]; FOR(i,1,cnt) dp[ma[i]] = max(dp[ma[i]], v[i]); for(i=n;i;--i) dp[i] = max(dp[i], dp[i + 1]); FOR(i,1,n) printf("%d\n", dp[i]); return 0; }
NSUBSTR - Substrings
You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as the maximal number of times that some string with length x appears in S. For example for string ‘ababa’ F(3) will be 2 because there is a string ‘aba’ that occurs twice. Your task is to output F(i) for every i so that 1<=i<=|S|.Input
String S consists of at most 250000 lowercase latin letters.Output
Output |S| lines. On the i-th line output F(i).Example
Input:
ababaOutput:
32
2
1
1
相关文章推荐
- python 笔记
- 字符串之一:匹配相关
- Android传感器使用大全
- HTML5第六次培训(AJAX基础-http协议)
- Java IO7:管道流、对象流
- 写出优美代码的两个方式:一步到位VS迭代优化
- VS2015 Update2中有关cordova和xamarin安装的问题
- 在windows下MySQLdb/MySQL-python的安装
- 59. Spiral Matrix II
- POJ-1019 Number Sequence
- RequireJS和JQuery的模块化编程
- 面试题:什么叫2B树
- JZOJ4446
- linux中文件压缩与解压缩学习心得
- 基于图的关键词抽取
- [Unity3D]Unity3D游戏开发之角色控制漫谈
- FZU2143 Board Game 【费用流+棋盘模型】
- mvc的视图中显示DataTable的方法
- NYOJ 1129 Salvation(技巧型深搜)
- Java高吞吐量系统设计优化建议