zoukankan      html  css  js  c++  java
  • [SPOJ-DISUBSTR]Distinct Substrings

    vjudge

    题意

    给你一个串,求不同字串个数。
    (nle10^5)

    sol

    直接建SAM然后输出(sum_{i=1}^{tot}len[i]-len[fa[i]])

    code

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    const int N = 1e5+5;
    int T,n,last=1,tot=1,tr[N][26],fa[N],len[N];
    char s[N];long long ans;
    void extend(int c)
    {
    	int v=last,u=++tot;last=u;
    	len[u]=len[v]+1;
    	while (v&&!tr[v][c]) tr[v][c]=u,v=fa[v];
    	if (!v) fa[u]=1;
    	else
    	{
    		int x=tr[v][c];
    		if (len[x]==len[v]+1) fa[u]=x;
    		else
    		{
    			int y=++tot;
    			memcpy(tr[y],tr[x],sizeof(tr[y]));
    			fa[y]=fa[x];fa[x]=fa[u]=y;len[y]=len[v]+1;
    			while (v&&tr[v][c]==x) tr[v][c]=y,v=fa[v];
    		}
    	}
    }
    int main()
    {
    	scanf("%d",&T);
    	while (T--)
    	{
    		scanf("%s",s+1);n=strlen(s+1);
    		ans=0;last=tot=1;
    		memset(tr,0,sizeof(tr));
    		memset(fa,0,sizeof(fa));
    		memset(len,0,sizeof(len));
    		for (int i=1;i<=n;++i) extend(s[i]-'A');
    		for (int i=1;i<=tot;++i) ans+=len[i]-len[fa[i]];
    		printf("%lld
    ",ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    【题解】B进制星球
    高斯消元
    7.16
    题解 P1572 【计算分数】
    7.30
    7.31
    项目中使用 MyBatis(一)
    从整体上了解Spring MVC
    Spring注解
    Spring IOC 和 DI
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8660176.html
Copyright © 2011-2022 走看看