zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    移动端自适应之flexible
    iview表单验证之正则验证、函数验证
    h5获取地理坐标
    $store.getters调用不执行
    arcgisJs之底图切换插件
    使用iview 的表单组件验证 Upload 组件
    ArcGis之popup列表字段自定义
    NHibernate 错误
    让C#可以像Javascript一样操作Json
    ZooKeeper学习第八期——ZooKeeper伸缩性
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8900891.html
Copyright © 2011-2022 走看看