CSU1632-Repeated Substrings

本文介绍了一种用于计算字符串中重复子串数量的高效算法。该算法适用于生物学和化学等领域的字符串分析任务,能够找出长度不超过100000的字符串中所有重复至少两次的子串,并统计其种类数量。

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

Repeated Substrings

Time Limit: 5 Sec   Memory Limit: 128 MB
Submit: 87   Solved: 35
[ Submit][ Status][ Web Board]

Description

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 input consists of at most 10 cases. 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

HINT

Source

题意:给出一个字符串,求出出现次数大于1的子串的有几种

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>

using namespace std;

const int N=200010;
const int INF=0x7FFFFFFF;

struct Sa
{
    char s[N];
    int rk[2][N],sa[N],h[N],w[N],now,n;
    int rmq[N][20],lg[N],bel[N];

    bool GetS()
    {
        scanf("%s",s+1);
        return true;
    }

    void getsa(int z,int &m)
    {
        int x=now,y=now^=1;
        for(int i=1; i<=z; i++) rk[y][i]=n-i+1;
        for(int i=1,j=z; i<=n; i++)
            if(sa[i]>z) rk[y][++j]=sa[i]-z;
        for(int i=1; i<=m; i++) w[i]=0;
        for(int i=1; i<=n; i++) w[rk[x][rk[y][i]]]++;
        for(int i=1; i<=m; i++) w[i]+=w[i-1];
        for(int i=n; i>=1; i--) sa[w[rk[x][rk[y][i]]]--]=rk[y][i];
        for(int i=m=1; i<=n; i++)
        {
            int *a=rk[x]+sa[i],*b=rk[x]+sa[i-1];
            rk[y][sa[i]]=*a==*b&&*(a+z)==*(b+z)?m-1:m++;
        }
    }

    void getsa(int m)
    {
        now=rk[1][0]=sa[0]=s[0]=0;
        n=strlen(s+1);
        for(int i=1; i<=m; i++) w[i]=0;
        for(int i=1; i<=n; i++) w[s[i]]++;
        for(int i=1; i<=m; i++) rk[1][i]=rk[1][i-1]+(bool)w[i];
        for(int i=1; i<=m; i++) w[i]+=w[i-1];
        for(int i=1; i<=n; i++) rk[0][i]=rk[1][s[i]];
        for(int i=1; i<=n; i++) sa[w[s[i]]--]=i;
        rk[1][n+1]=rk[0][n+1]=0;
        for(int x=1,y=rk[1][m]; x<=n&&y<=n; x<<=1) getsa(x,y);
        for(int i=1,j=0; i<=n; h[rk[now][i++]]=j?j--:j)
        {
            if(rk[now][i]==1) continue;
            int k=n-max(sa[rk[now][i]-1],i);
            while(j<=k&&s[sa[rk[now][i]-1]+j]==s[i+j]) ++j;
        }
    }

    void getrmq()
    {
        h[n+1]=h[1]=lg[1]=0;
        for(int i=2; i<=n; i++)
            rmq[i][0]=h[i],lg[i]=lg[i>>1]+1;
        for(int i=1; (1<<i)<=n; i++)
        {
            for(int j=2; j<=n; j++)
            {
                if(j+(1<<i)>n+1) break;
                rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<i-1)][i-1]);
            }
        }
    }

    int lcp(int x,int y)
    {
        int l=min(rk[now][x],rk[now][y])+1,r=max(rk[now][x],rk[now][y]);
        return min(rmq[l][lg[r-l+1]],rmq[r-(1<<lg[r-l+1])+1][lg[r-l+1]]);
    }

    void work()
    {
        getsa(300);
        int ans=h[2];
        for(int i=3;i<=n;i++)
            if(h[i]>h[i-1])
                ans+=h[i]-h[i-1];
        printf("%d\n",ans);
    }

} sa;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        sa.GetS();
        sa.work();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值