• Luogu3804:[模板]后缀自动机


    题面

    luogu

    Sol

    (sam)然后树形(DP)
    当时还不会拓扑排序的我

    # include <bits/stdc++.h>
    # define IL inline
    # define RG register
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    
    template <class Int>
    IL void Input(RG Int &x){
    	RG int z = 1; RG char c = getchar(); x = 0;
    	for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    	for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    	x *= z;
    }
    
    const int maxn(2e6 + 5);
    
    int n, trans[26][maxn], fa[maxn], len[maxn], tot = 1, last = 1, size[maxn];
    ll ans;
    vector <int> edge[maxn];
    char s[maxn];
    
    IL void Extend(RG int c){
    	RG int np = ++tot, p = last; last = np;
    	len[np] = len[p] + 1, size[np] = 1;
    	while(p && !trans[c][p]) trans[c][p] = np, p = fa[p];
    	if(!p) fa[np] = 1;
    	else{
    		RG int q = trans[c][p];
    		if(len[p] + 1 == len[q]) fa[np] = q;
    		else{
    			RG int nq = ++tot;
    			len[nq] = len[p] + 1, fa[nq] = fa[q];
    			for(RG int i = 0; i < 26; ++i) trans[i][nq] = trans[i][q];
    			fa[q] = fa[np] = nq;
    			while(p && trans[c][p] == q) trans[c][p] = nq, p = fa[p];
    		}
    	}
    }
    
    IL void Dfs(RG int x){
    	for(RG int l = edge[x].size(), e = 0; e < l; ++e)
    		Dfs(edge[x][e]), size[x] += size[edge[x][e]];
    	if(size[x] != 1) ans = max(ans, 1LL * size[x] * len[x]);
    }
    
    int main(RG int argc, RG char* argv[]){
    	scanf(" %s", s), n = strlen(s);
    	for(RG int i = 0; i < n; ++i) Extend(s[i] - 'a');
    	for(RG int i = 2; i <= tot; ++i) edge[fa[i]].push_back(i);
    	Dfs(1);
    	printf("%lld
    ", ans);
    	return 0;
    }
    
  • 相关阅读:
    Android之Margin和Padding属性及支持的长度单位
    java jvm eclipse 性能调优
    spring aop 内部方法调用事务不生效问题解决
    服务器 获取用户 真实ip
    Nginx gzip配置
    全局唯一的支付和订单id生成算法
    spring aop 方法增加日志记录
    linux cp复制文件 直接覆盖
    Twitter分布式自增ID算法snowflake原理解析
    nginx 命令
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8900891.html
Copyright © 2020-2023  润新知