UVALive 6869 Repeated Substrings(后缀数组)

本文介绍了一种高效算法,用于计算一个给定字符串中所有出现至少两次的不同子串的数量。该算法利用后缀数组和高度数组来实现,能够处理长度高达10万的字符串,并确保结果能在32位整数范围内。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

String analysis often arises in applications from biology and
chemistry, such as the study of DNA and protein molecules. One
interesting problem is to find how many substrings are repeated
(at least twice) in a long string.
In this problem, you will write a program to find the total
number of repeated substrings in a string of at most 100 000
alphabetic characters. Any unique substring that occurs more
than once is counted. As an example, if the string is “aabaab”,
there are 5 repeated substrings: “a”, “aa”, “aab”, “ab”, “b”.
If the string is “aaaaa”, the repeated substrings are “a”, “aa”, “aaa”, “aaaa”. Note that repeated
occurrences of a substring may overlap (e.g. “aaaa” in the second case).
Input
The first line contains a positive integer, specifying the number of cases to follow. Each of the following
line contains a nonempty string of up to 100 000 alphabetic characters.
Output
For each line of input, output one line containing the number of unique substrings that are repeated.
You may assume that the correct answer fits in a signed 32-bit integer.
Sample Input
3
aabaab
aaaaa
AaAaA
Sample Output
5
4
5

/*
后缀数组.
题意:求出现过两次以上的不同子串有多少种.
字串即字符串后缀的前缀.
ans+=ht[i]-ht[i-1].
用当前的贡献减去之前已经计算过的贡献.
然后把每个前缀的贡献累加.
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 100010
using namespace std;
int n,K,m=130,ans,sa[MAXN],rank1[MAXN],c[MAXN],ht[MAXN],t1[MAXN],t2[MAXN],s[MAXN];
char ch[MAXN];
bool cmp(int *y,int a,int b,int k)
{
    int a1=y[a],b1=y[b];
    int a2=a+k>=n?-1:y[a+k];
    int b2=b+k>=n?-1:y[b+k];
    return a1==b1&&a2==b2;
}
void slovesa()
{
    int *x=t1,*y=t2;
    for(int i=0;i<m;i++) c[i]=0;
    for(int i=0;i<n;i++) c[x[i]=s[i]]++;
    for(int i=1;i<m;i++) c[i]+=c[i-1];
    for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
    for(int k=1,p=0;k<=n;k<<=1,m=p,p=0)
    {
        for(int i=n-k;i<n;i++) y[p++]=i;
        for(int i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
        for(int i=0;i<m;i++) c[i]=0;
        for(int i=0;i<n;i++) c[x[y[i]]]++;
        for(int i=1;i<m;i++) c[i]+=c[i-1];
        for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
        swap(x,y),p=1,x[sa[0]]=0;
        for(int i=1;i<n;i++)
        {
            if(cmp(y,sa[i-1],sa[i],k)) x[sa[i]]=p-1;
            else x[sa[i]]=p++;
        }
        if(p>=n) break;
    }
}
void sloveheight()
{
    int k=0;
    for(int i=0;i<n;i++) rank1[sa[i]]=i;
    for(int i=0;i<n;ht[rank1[i++]]=k)
    {
        int j=sa[rank1[i]-1];
        if(k) k--;
        while(j+k<n&&i+k<n&&s[i+k]==s[j+k]) k++;
    }
    ht[0]=0;
}
void slove()
{
    for(int i=1;i<n;i++)
    {
        int l=n-sa[i-1]-1;
        if(ht[i]>=ht[i-1]) ans+=ht[i]-ht[i-1];
    }
    printf("%d\n",ans);
}
void Clear()
{
    ans=0,m=130;
    memset(sa,0,sizeof sa);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",ch);Clear();
        n=strlen(ch);
        for(int i=0;i<n;i++) s[i]=ch[i];
        s[n++]=0;
        slovesa(),sloveheight(),slove();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值