码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷3804 【模板】后缀自动机

时间:2018-01-16 18:32:59      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:amp   log   http   自动   lld   scan   detail   details   sdn   

题目

给定一个只包含小写字母的字符串SS,

请你求出 SS 的所有出现次数不为 11 的子串的出现次数乘上该子串长度的最大值。

输入格式

一行一个仅包含小写字母的字符串SS

输出格式

一个整数,为 所求答案

输入样例

abab

输出样例

4

提示

对于\(10\%10%\)的数据,|S|<=1000∣S∣<=1000
对于\(100\%100%\)的数据,|S|<=\(10^6\)∣S∣<=\(10^6\)

题解

贴个模板。。。
这篇讲得不错
lible的讲解

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u]; k; k = ed[k].nxt)
using namespace std;
const int maxn = 2000005,maxm = 100005,INF = 1000000000;
inline int RD(){
    int out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57) {if (c == ‘-‘) flag = -1; c = getchar();}
    while (c >= 48 && c <= 57) {out = (out << 1) + (out << 3) + c - ‘0‘; c = getchar();}
    return out * flag;
}
int ch[maxn][26],pre[maxn],step[maxn],n,cnt,last;
int b[maxn],sz[maxn],a[maxn];
LL ans = 0;
char s[maxn];
void ins(int u){
    int p = last,np = ++cnt;
    last = np; step[np] = step[p] + 1;
    while (p && !ch[p][u]) ch[p][u] = np,p = pre[p];
    if (!p) pre[np] = 1;
    else {
        int q = ch[p][u];
        if (step[q] == step[p] + 1) pre[np] = q;
        else {
            int nq = ++cnt; step[nq] = step[p] + 1;
            for (int i = 0; i < 26; i++) ch[nq][i] = ch[q][i];
            pre[nq] = pre[q]; pre[q] = pre[np] = nq;
            while (ch[p][u] == q) ch[p][u] = nq,p = pre[p];
        }
    }
    sz[np] = 1;
}
void solve(){
    REP(i,cnt) b[step[i]]++;
    REP(i,cnt) b[i] += b[i - 1];
    REP(i,cnt) a[b[step[i]]--] = i;
    for (int i = cnt; i; i--){
        sz[pre[a[i]]] += sz[a[i]];
        if (sz[a[i]] > 1) ans = max(ans,1ll * step[a[i]] * sz[a[i]]);
    }
}
int main(){
    scanf("%s",s + 1);
    cnt = last = 1; n = strlen(s + 1);
    for (int i = 1; i <= n; i++) ins(s[i] - ‘a‘);
    solve();
    printf("%lld",ans);
    return 0;
}

洛谷3804 【模板】后缀自动机

标签:amp   log   http   自动   lld   scan   detail   details   sdn   

原文地址:https://www.cnblogs.com/Mychael/p/8296124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!