SPOJ 8222 Substrings
题面
Description
给长度为 n 的字符串 S , 对任意的 L , 求长度为 L 的子串最多出现的次数.
Input
String S consists of at most 250000 lowercase latin letters.
Output
Output |S| lines. On the i-th line output F(i).
Sample Input
ababa
Sample Output
3
2
2
1
1
题解
后缀自动机统计子串出现次数的应用.
考虑我们插入一个节点时, 其suffix link上的所有节点所代表的字符串的出现次数都+1, 因此parent tree上一个节点所代表的字符串的出现次数等于以它为根的子树中实点的出现次数.
线段树上成段更新来维护即可.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
const int LEN = 250000;
struct segmentTree
{
int a[LEN << 2];
inline segmentTree()
{
memset(a, 0, sizeof(a));
}
void modify(int u, int curL, int curR, int L, int R, int w)
{
if(curL >= L && curR <= R)
{
a[u] = std::max(a[u], w);
return;
}
int mid = curL + curR >> 1;
if(L <= mid)
modify(u << 1, curL, mid, L, R, w);
if(R > mid)
modify(u << 1 | 1, mid + 1, curR, L, R, w);
}
inline void modify(int L, int R, int w)
{
modify(1, 1, LEN, L, R, w);
}
int query(int u, int L, int R, int pos)
{
if(L == R)
return a[u];
int mid = L + R >> 1;
if(pos <= mid)
return std::max(a[u], query(u << 1, L, mid, pos));
else
return std::max(a[u], query(u << 1 | 1, mid + 1, R, pos));
}
inline int query(int pos)
{
return query(1, 1, LEN, pos);
}
}sgt;
struct suffixAutomaton
{
struct state
{
state *suc[26], *pre;
std::vector<state*> sucOnTr;
int len, sz, tg;
inline state()
{
for(int i = 0; i < 26; ++ i)
suc[i] = NULL;
sucOnTr.clear();
sz = tg = 0;
}
};
state *rt, *lst;
inline void insert(int c)
{
state *u = new state;
u->len = lst->len + 1;
u->sz = 1;
for(; lst != NULL && lst->suc[c] == NULL; lst->suc[c] = u, lst = lst->pre);
if(lst == NULL)
u->pre = rt;
else
{
state *p = lst->suc[c];
if(p->len == lst->len + 1)
u->pre = p;
else
{
state *q = new state;
*q = *p;
q->sz = 0;
q->len = lst->len + 1;
p->pre = u->pre = q;
for(; lst != NULL && lst->suc[c] == p; lst->suc[c] = q, lst = lst->pre);
}
}
lst = u;
}
inline void build(char *str, int len)
{
lst = rt = new state;
rt->len = 0, rt->pre = NULL;
for(int i = 0; i < len; ++ i)
insert(str[i] - 'a');
}
void getSucessorOnSuffixTree(state *u)
{
u->tg = 1;
if(u->pre != NULL)
u->pre->sucOnTr.push_back(u);
for(int i = 0; i < 26; ++ i)
if(u->suc[i] != NULL && ! u->suc[i]->tg)
getSucessorOnSuffixTree(u->suc[i]);
}
void DFS(state *u)
{
for(std::vector<state*>::iterator p = u->sucOnTr.begin(); p != u->sucOnTr.end(); ++ p)
DFS(*p), u->sz += (*p)->sz;
if(u != rt)
sgt.modify(u->pre->len + 1, u->len, u->sz);
}
inline void work()
{
getSucessorOnSuffixTree(rt);
DFS(rt);
}
}SAM;
int main()
{
#ifndef ONLINE_JUDGE
freopen("SPOJ8222.in", "r", stdin);
#endif
static char str[LEN];
scanf("%s", str);
int len = strlen(str);
SAM.build(str, len);
SAM.work();
for(int i = 1; i <= len; ++ i)
printf("%d\n", sgt.query(i));
}
SPOJ 8222 Substrings的更多相关文章
- spoj 8222 Substrings (后缀自动机)
spoj 8222 Substrings 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length(S)) 解题思路:我们构造S的SAM,那么对于 ...
- SPOJ 8222 Substrings(后缀自动机)
[题目链接] http://www.spoj.com/problems/NSUBSTR/ [题目大意] 给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值. 求出所有的F. [题 ...
- spoj 8222 Substrings(后缀自动机+DP)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28005 [题意] 给一个字符串S,令F(x)表示S的所有长度为 ...
- SPOJ 8222. Substrings(后缀自动机模板)
后缀自动机+dp. 后缀自动机主要是在functioner大牛那里学习的:http://blog.sina.com.cn/s/blog_70811e1a01014dkz.html 这道题是在No_st ...
- 【SPOJ】Substrings(后缀自动机)
[SPOJ]Substrings(后缀自动机) 题面 Vjudge 题意:给定一个长度为\(len\)的串,求出长度为1~len的子串中,出现最多的出现了多少次 题解 出现次数很好处理,就是\(rig ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
- ●SPOJ 8222 NSUBSTR–Substrings
题链: http://www.spoj.com/problems/NSUBSTR/题解: 后缀自动机. 不难发现,对于自动机里面的一个状态s, 如果其允许的最大长度为maxs[s],其right集合的 ...
- ●SPOJ 8222 NSUBSTR - Substrings(后缀数组)
题链: http://www.spoj.com/problems/NSUBSTR/ 题解: 同届红太阳 --WSY给出的后缀数组解法!!! 首先用倍增算法求出 sa[i],rak[i],hei[i]然 ...
- ●SPOJ 8222 NSUBSTR–Substrings(后缀自动机)
题链: http://www.spoj.com/problems/NSUBSTR/ 题解: 后缀自动机的水好深啊!懂不了相关证明,带着结论把这个题做了.看来这滩深水要以后再来了. 本题要用到一个叫 R ...
随机推荐
- 转:跟我一起写Makefile (PDF重制版)
原文地址:http://seisman.info/how-to-write-makefile.html 其它一些问题 不妨看一下:http://blog.csdn.net/huyansoft/art ...
- PAT Basic 1076
1076 Wifi密码 下面是微博上流传的一张照片:“各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请 ...
- UVa 1629 DP Cake slicing
题意: 一块n×m的蛋糕上有若干个樱桃,要求切割若干次以后,每块蛋糕上有且仅有1个樱桃.求最小的切割长度. 分析: d(u, d, l, r)表示切割矩形(u, d, l, r)所需要的最小切割长度. ...
- Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies
Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies. API 调用退出异常. (Except ...
- 从几率到logisitic函数
odds 几率,又称事件的优势比.几率和概率的关系如下: o=p1−pp=o1+o Logistic 回归模型的因变量只有 1/0 两种取值.假设在 p 个独立自变量 x1,x2,…,xp 作用下,记 ...
- [python subprocess学习篇] 调用系统命令
http://www.jb51.net/article/57208.htm 3).Popen.communicate(input=None):与子进程进行交互.向stdin发送数据,或从stdout和 ...
- PHP7异常处理
try { // Code that may throw an Exception or Error. }catch (Exception $e) { } catch (Error $t) { } p ...
- 利用hibernate与struts框架制作简单注册界面
一:配置hibernate 1.导包 hibernate包和jdbc连接mysql数据库的包 2.实用工具生成hibernate配置文件和映射文件 3.做好hibernateUtil生成session ...
- Welcome-to-Swift-02基本运算符
运算符是检查,改变,合并值的特殊符号或短语.例如,加号+将两个数相加(如let i = 1 + 2).复杂些的运行算例如逻辑与运算符&&(如if enteredDoorCode &am ...
- hosts文件位置
windows C:\WINDOWS\system32\drivers\etc mac /etc/hosts 修改hosts文件会遇到无法保存的问题,解法方法参考下文 http://mtoou.inf ...