zoukankan      html  css  js  c++  java
  • 洛谷.3804.[模板]后缀自动机

    题目链接

    //1660ms	229.72MB
    //靠出现次数大于1没判。。我说呢。。
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    const int N=(1e6+5)*2;
    
    struct Suffix_Automaton
    {
    	int tot,las,fa[N],len[N],son[N][26],right[N],A[N],tm[N];
    	char s[N>>1];
    
    	void Insert(int c)
    	{
    		int np=++tot,p=las; len[las=np]=len[p]+1;
    		for(; p&&!son[p][c]; p=fa[p]) son[p][c]=np;
    		if(!p) fa[np]=1;
    		else
    		{
    			int q=son[p][c];
    			if(len[q]==len[p]+1) fa[np]=q;
    			else
    			{
    				int nq=++tot; len[nq]=len[p]+1;
    				memcpy(son[nq],son[q],sizeof son[q]);
    				fa[nq]=fa[q], fa[q]=fa[np]=nq;
    				for(; son[p][c]==q; p=fa[p]) son[p][c]=nq;
    			}
    		}
    		right[np]=1;
    	}
    	void Solve()
    	{
    		scanf("%s",s+1);
    		int l=strlen(s+1); tot=las=1;
    		for(int i=1; i<=l; ++i) Insert(s[i]-'a');
    
    		for(int i=1; i<=tot; ++i) ++tm[len[i]];
    		for(int i=1; i<=l; ++i) tm[i]+=tm[i-1];
    		for(int i=tot; i; --i) A[tm[len[i]]--]=i;
    
    //		for(int p=1,i=1; i<=l; ++i) ++right[p=son[p][s[i]-'a']];
    		for(int i=tot,x=A[i]; i; --i,x=A[i]) right[fa[x]]+=right[x];
    		long long res=0;
    		for(int i=2; i<=tot; ++i)
    			if(right[i]>1) res=std::max(res,1ll*right[i]*len[i]);
    		printf("%lld",res);
    	}
    }sam;
    
    int main()
    {
    	sam.Solve();
    	return 0;
    }
    
  • 相关阅读:
    oracle查询表最后的操作时间
    设置tomcat开机自启
    jmeter 连接mysql
    ubuntu卸载软件
    转 ubuntu 安装chrome 和chromedriver
    转 ps -ef ps -aux 区别
    ubuntu 20 jenkins 开机启动
    Ubuntu20.04安装JDK
    ubuntu 安装指定版本gitlab
    Gitlab备份和恢复操作记录 转
  • 原文地址:https://www.cnblogs.com/SovietPower/p/9164446.html
Copyright © 2011-2022 走看看