SPOJ 705 Distinct Substrings(后缀数组)

【题目链接】 http://www.spoj.com/problems/SUBST1/

【题目大意】

  给出一个串,求出不相同的子串的个数。

【题解】

  对原串做一遍后缀数组,按照后缀的名次进行遍历,
  每个后缀对答案的贡献为n-sa[i]+1-h[i],
  因为排名相邻的后缀一定是公共前缀最长的,
  那么就可以有效地通过LCP去除重复计算的子串。

【代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1000005;
int T,Cas=1,n,Rank[N],sa[N],h[N],tmp[N],cnt[N],nxt[N];
char c,s[N];
void suffixarray(int n,int m){
    int i,j,k;n++;
    for(i=0;i<2*n+5;i++)Rank[i]=sa[i]=h[i]=tmp[i]=0;
    for(i=0;i<m;i++)cnt[i]=0;
    for(i=0;i<n;i++)cnt[Rank[i]=s[i]]++;
    for(i=1;i<m;i++)cnt[i]+=cnt[i-1];
    for(i=0;i<n;i++)sa[--cnt[Rank[i]]]=i;
    for(k=1;k<=n;k<<=1){
        for(i=0;i<n;i++){
            j=sa[i]-k;
            if(j<0)j+=n;
            tmp[cnt[Rank[j]]++]=j;
        }sa[tmp[cnt[0]=0]]=j=0;
        for(i=1;i<n;i++){
            if(Rank[tmp[i]]!=Rank[tmp[i-1]]||Rank[tmp[i]+k]!=Rank[tmp[i-1]+k])cnt[++j]=i;
            sa[tmp[i]]=j;
        }memcpy(Rank,sa,n*sizeof(int));
        memcpy(sa,tmp,n*sizeof(int));
        if(j>=n-1)break;
    }for(j=Rank[h[i=k=0]=0];i<n-1;i++,k++)
    while(~k&&s[i]!=s[sa[j-1]+k])h[j]=k--,j=Rank[sa[j]+1];
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%s",s);
        n=strlen(s);
        suffixarray(n,256);
        long long ans=0;
        for(int i=1;i<=n;i++)ans=ans+n-sa[i]-h[i];
        printf("%lld\n",ans);
    }return 0;
}

  

时间: 2024-08-02 02:50:32

SPOJ 705 Distinct Substrings(后缀数组)的相关文章

SPOJ 694 || 705 Distinct Substrings ( 后缀数组 &amp;&amp; 不同子串的个数 )

题意 : 对于给出的串,输出其不同长度的子串的种类数 分析 : 有一个事实就是每一个子串必定是某一个后缀的前缀,换句话说就是每一个后缀的的每一个前缀都代表着一个子串,那么如何在这么多子串or后缀的前缀中找出不同的并计数呢?思路就是所有的可能子串数 - 重复的子串数.首先我们容易得到一个长度为 len 的串的子串数为 len * ( len + 1) / 2.那如何知道重复的子串数呢?答案就是利用后缀数组去跑一遍 Height ,得到所有的最长公共前缀(LCP),这些最长公共前缀的值都存在了 He

spoj 694 Distinct Substrings 后缀数组

题目链接 求一个字符串中不相同的子串的个数. 子串的总数是(n+1)*n/2, 减去height[i]就可以. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include

SPOJ 694. Distinct Substrings,705. New Distinct Substrings(后缀数组)

题目大意:给定长度为N的字符串,求出其中不相同子串的个数. 解题思路:每一个字串一定是某个后缀的前缀,那么原问题就可以等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照suffix(sa[1]),suffix(sa[2])--suffix(sa[n])的顺序计算,我们会发现对于每个新加进来的后缀suffix(sa[k]),它将产生n-sa[k]+1个新的前缀.但是其中有leight[k]个是和前面的字符串的前缀是相同的.所以suffix(sa[k])加进来增加的不同的子串的个数为n-s

spoj Distinct Substrings 后缀数组

给定一个字符串,求不相同的子串的个数. 假如给字符串“ABA";排列的子串可能: A B A AB  BA ABA 共3*(3+1)/2=6种; 后缀数组表示时: A ABA BA 对于A和AB height[i]=1; 表明一个长度公共,所以ABA中多出现了A这个子串,所以6-1=5: 对于ABA BA height[i]=0,所以不需要减去. 最后答案为5: #include<iostream> #include<stdio.h> #include<string

SPOJ694&amp;&amp;SPOJ705:Distinct Substrings(后缀数组)

Description Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 1000 Output For each test case output one number saying the numb

SPOJ694--- DISUBSTR - Distinct Substrings(后缀数组)

Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 1000 Output For each test case output one number saying the number of distin

[spoj694&amp;spoj705]New Distinct Substrings(后缀数组)

题意:求字符串中不同子串的个数. 解题关键:每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相同的前缀的个数. 1.总数减去height数组的和即可. 注意这里height中为什么不需要进行组合计数,因为,每一个height的左端点已经确定,所以只需变动右端点,总共$height[i]$种情况. 2.如果所有的后缀按照 suffix(sa[1]), suffix(sa[2]),suffix(sa[3]), …… ,suffix(sa[n])的顺序计算,不难发现,对于每一次新加进来

SPOJ 694、705 Distinct Substrings 、 New Distinct Substrings (后缀数组)

题目大意: 求串中不同的子串的个数. 思路分析: 子串一定是某一个后缀的前缀. 所以我们把每一个后缀拿出来,分析它有多少个前缀,然后除去它与sa数组中前面那个后缀相同的前缀. 最后也就是 ans = segma (n-sa[i] + height[i]).... #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define maxn 1000005

【SPOJ】Distinct Substrings(后缀自动机)

[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/endpos\)集合的大小 但是实际上我们没有任何必要减去不合法的数量 我们只需要累加每个节点表示的合法子串的数量即可 这个值等于\(longest-shortest+1=longest-parent.longest\) #include<iostream> #include<cstdio&g