zoukankan      html  css  js  c++  java
  • 【洛谷 P3804】 【模板】后缀自动机

    题目链接

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int MAXN = 1000010;
    struct SAM{
        int ch[26];
        int len, fa;
    }sam[MAXN << 1];
    int las = 1, cnt = 1, f[MAXN << 1];
    struct Edge{
        int next, to;
    }e[MAXN << 1];
    int head[MAXN << 1], num;
    inline void Add(int from, int to){
        e[++num].to = to; e[num].next = head[from]; head[from] = num;
    }
    inline void add(int c){
        int p = las; int np = las = ++cnt;
        sam[np].len = sam[p].len + 1; f[cnt] = 1;
        for(; p && !sam[p].ch[c]; p = sam[p].fa) sam[p].ch[c] = np;
        if(!p) sam[np].fa = 1;
        else{
            int q = sam[p].ch[c];
            if(sam[q].len == sam[p].len + 1) sam[np].fa = q;
            else{
                int nq = ++cnt; sam[nq] = sam[q];
                sam[nq].len = sam[p].len + 1;
                sam[q].fa = sam[np].fa = nq;
                for(; p && sam[p].ch[c] == q; p = sam[p].fa) sam[p].ch[c] = nq;
            }
        }
    }
    char a[MAXN];
    long long ans;
    void dfs(int u){
        for(int i = head[u]; i; i = e[i].next){
            dfs(e[i].to);
            f[u] += f[e[i].to];
        }
        if(f[u] != 1) ans = max(ans, (long long)sam[u].len * f[u]);
    }
    int main(){
        scanf("%s", a + 1);
        int len = strlen(a + 1);
        for(int i = 1; i <= len; ++i)
           add(a[i] - 'a');
        for(int i = 2; i <= cnt; ++i)
            Add(sam[i].fa, i);
        dfs(1);
        printf("%lld
    ", ans);
        return 0;
    }
    
  • 相关阅读:
    沟通的5个步骤
    安家博客园,发表感想
    postman 简单教程-实现简单的接口测试
    Postman的基本使用
    面试题-如何测试一个APP
    Fiddler快捷方式导出jmeter脚本,傻瓜式
    Servlet学习(三)
    scala学习(一)
    白底抠图
    Servlet学习(二)
  • 原文地址:https://www.cnblogs.com/Qihoo360/p/10985926.html
Copyright © 2011-2022 走看看