(color{#0066ff}{ 题目描述 })
你得到一个字符串,最多由25万个小写拉丁字母组成。我们将 F(x)定义为某些长度X的字符串在s中出现的最大次数,例如字符串'ababaf'- F(x),因为有一个字符串'ABA'出现两次。你的任务是输出 F(x)每一个I,以使1<=i<=|S|.
(color{#0066ff}{输入格式})
一个字符串
(color{#0066ff}{输出格式})
每行输出一个数(F(i))
(color{#0066ff}{输入样例})
ababa
(color{#0066ff}{输出样例})
3
2
2
1
1
(color{#0066ff}{数据范围与提示})
none
(color{#0066ff}{ 题解 })
建立SAM,出现次数就是叶子节点个数
鸡排之后倒着递推siz,并用其更新答案就行了
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL in() {
char ch; int x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 5e5 + 5;
struct SAM {
protected:
struct node {
node *ch[26], *fa;
int len, siz;
node(int len = 0, int siz = 0): fa(NULL), len(len), siz(siz) {
memset(ch, 0, sizeof ch);
}
};
node *root, *tail, *lst;
node pool[maxn], *id[maxn];
int c[maxn];
void extend(int c) {
node *o = new(tail++) node(lst->len + 1, 1), *v = lst;
for(; v && !v->ch[c]; v = v->fa) v->ch[c] = o;
if(!v) o->fa = root;
else if(v->len + 1 == v->ch[c]->len) o->fa = v->ch[c];
else {
node *n = new(tail++) node(v->len + 1), *d = v->ch[c];
std::copy(d->ch, d->ch + 26, n->ch);
n->fa = d->fa, d->fa = o->fa = n;
for(; v && v->ch[c] == d; v = v->fa) v->ch[c] = n;
}
lst = o;
}
void clr() {
tail = pool;
root = lst = new(tail++) node();
}
public:
SAM() { clr(); }
void ins(char *s) { for(char *p = s; *p; p++) extend(*p - 'a'); }
void getid() {
int maxlen = 0;
for(node *o = pool; o != tail; o++) c[o->len]++, maxlen = std::max(maxlen, o->len);
for(int i = 1; i <= maxlen; i++) c[i] += c[i - 1];
for(node *o = pool; o != tail; o++) id[--c[o->len]] = o;
}
void getans(int *ans) {
for(int i = tail - pool - 1; i; i--) {
node *o = id[i];
if(o->fa) o->fa->siz += o->siz;
ans[o->len] = std::max(ans[o->len], o->siz);
}
}
}sam;
char s[maxn];
int ans[maxn];
int main() {
scanf("%s", s);
sam.ins(s);
sam.getid();
sam.getans(ans);
for(int i = 0, len = strlen(s); i < len; i++) printf("%d
", ans[i + 1]);
return 0;
}