标签:说明 lld 优秀 == return 大于 node while 空间
给定一个只包含小写字母的字符串\(S\),
请你求出 \(S\) 的所有出现次数不为 \(1\) 的子串的出现次数乘上该子串长度的最大值。
一行一个仅包含小写字母的字符串\(S\)
一个整数,为 所求答案
abab
4
对于\(10\%\)的数据,\(∣S∣\leq 1000\)
对于\(100\%\)的数据,\(|S|\leq 10^6\)
#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 = 2e6 + 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'); }
LL getans() {
LL ans = 0;
int len = tail - pool, 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;
for(int i = len - 1; i; i--) {
node *o = id[i];
o->fa->siz += o->siz;
if(o->siz > 1) ans = std::max(ans, 1LL * o->siz * o->len);
}
return ans;
}
}sam;
int main() {
static char s[maxn];
scanf("%s", s);
sam.ins(s);
printf("%lld", sam.getans());
return 0;
}
标签:说明 lld 优秀 == return 大于 node while 空间
原文地址:https://www.cnblogs.com/olinr/p/10251693.html