• 【BZOJ4566】找相同字符(后缀自动机)


    【BZOJ4566】找相同字符(后缀自动机)

    题面

    BZOJ

    题解

    看到多串处理,(SA)就连起来
    (SAM???)
    单串建自动机
    然后其他串匹配

    对于一个串建完(SAM)
    另一个串在(SAM)上匹配
    记录当前匹配的最大长度

    匹配了当前位置的话,就能产生一定的贡献
    但是很显然,沿着(parent)往上,所有点都能够产生贡献
    所以匹配完再沿着(parent)做一遍类似(dp)的东西算贡献

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<vector>
    #include<queue>
    using namespace std;
    #define MAX 220000
    inline int read()
    {
        int x=0,t=1;char ch=getchar();
        while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
        if(ch=='-')t=-1,ch=getchar();
        while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
        return x*t;
    }
    struct Node
    {
    	int son[26];
    	int ff,len;
    }t[MAX<<1];
    int last=1,tot=1;
    int size[MAX<<1];
    long long ans;
    int f[MAX<<1],g[MAX<<1],a[MAX<<1],c[MAX<<1];
    char ch[MAX];
    void extend(int c)
    {
    	int p=last,np=++tot;last=np;
    	t[np].len=t[p].len+1;
    	while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
    	if(!p)t[np].ff=1;
    	else
    	{
    		int q=t[p].son[c];
    		if(t[q].len==t[p].len+1)t[np].ff=q;
    		else
    		{
    			int nq=++tot;
    			t[nq]=t[q];
    			t[nq].len=t[p].len+1;
    			t[q].ff=t[np].ff=nq;
    			while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
    		}
    	}
    	size[np]=1;
    }
    int main()
    {
    	scanf("%s",ch+1);
    	for(int i=1,l=strlen(ch+1);i<=l;++i)extend(ch[i]-97);
    	for(int i=1;i<=tot;++i)c[t[i].len]++;
    	for(int i=1;i<=tot;++i)c[i]+=c[i-1];
    	for(int i=1;i<=tot;++i)a[c[t[i].len]--]=i;
    	for(int i=tot;i;--i)size[t[a[i]].ff]+=size[a[i]];
    	scanf("%s",ch+1);
    	for(int i=1,l=strlen(ch+1),now=1,len=0;i<=l;++i)
    	{
    		int c=ch[i]-97;
    		if(t[now].son[c])++len,now=t[now].son[c];
    		else
    		{
    			while(now&&!t[now].son[c])now=t[now].ff;
    			if(!now)now=1,len=0;
    			else len=t[now].len+1,now=t[now].son[c];
    		}
    		ans+=1ll*size[now]*(len-t[t[now].ff].len);
    		g[now]++;
    	}
    	for(int i=tot;i;--i)f[t[a[i]].ff]+=f[a[i]]+g[a[i]];
    	for(int i=1;i<=tot;++i)ans+=1ll*size[i]*f[i]*(t[i].len-t[t[i].ff].len);
    	printf("%lld
    ",ans);
    	return 0;
    }
    
    
  • 相关阅读:
    html笔记
    Git入门学习总结
    使用OpenSSH远程管理Linux服务器
    Linux 网卡驱动的安装
    vi的使用
    Linux下常用的数据恢复工具
    网络文件系统(NFS)的使用
    文件系统管理
    磁盘存储管理
    用户权限管理
  • 原文地址:https://www.cnblogs.com/cjyyb/p/8449345.html
Copyright © 2020-2023  润新知