K - Repeated Substrings

题目

Given an input string composed solely of lowercase English letters, find the longest substring that occurs more than once in the input string. The two occurrences are allowed to partially overlap.
Input
The input is a single line containing a string of lowercase letters. The string contains more than one character, but no more than 105. At least one letter will appear at least twice.
Output
Print a single line of output: the longest substring that occurs more than once in the input string. If there are multiple longest repeated substrings, print the one the would come first when the longest substrings are sorted in lexicographical (alphabetical) order.

解题思路

求一个子串的出现次数类的问题多为后缀数组;
接下来发现需要求一个子串出现此时的最大值,也就可以通过二分子串长度来判断该是否存在合法子串
最后一步寻找后缀数组最小的结果也就是字典序最小的结果

AC代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int s[maxn];
int sa[maxn];
int t1[maxn],t2[maxn],c[maxn];
int rk[maxn],height[maxn];
void build(int n,int m)
    {
        int i,j,p,*x=t1,*y=t2;
        for(i=0;i<m;i++)c[i]=0;
        for(i=0;i<n;i++)c[x[i]=s[i]]++;
        for(i=1;i<m;i++)c[i]+=c[i-1];
        for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
        for(j=1;j<=n;j<<=1)
        {
            p=0;
            for(i=n-j;i<n;i++)y[p++]=i;
            for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
            for(i=0;i<m;i++)c[i]=0;
            for(i=0;i<n;i++)c[x[y[i]]]++;
            for(i=1;i<m;i++)c[i]+=c[i-1];
            for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
            swap(x,y);
            p=1;x[sa[0]]=0;
            for(i=1;i<n;i++)
                x[sa[i]]=y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
            if(p>=n)break;
            m=p;
        }
    }
void getHeight(int s[],int n)
    {
        int i,j,k=0;
        for(i=0;i<=n;i++)rk[sa[i]]=i;
        for(i=0;i<n;i++)
        {
            if(k)k--;
            j=sa[rk[i]-1];
            while(s[i+k]==s[j+k])k++;
            height[rk[i]]=k;
        }
    }


bool check(int n,int len)
{
    int cnt=1;
    for(int i=2;i<=n;i++)
    {
        if(height[i]>=len)
        {
            cnt++;
            if(cnt>=2) return true;
        }
        else cnt=1;
    }
    return false;
}
char S[maxn];

int main()
{
    int mx=0;
    scanf("%s",S);
    int n=strlen(S);
    for(int i=0;i<n;i++)
    {
        s[i]=S[i]-'a'+1;
        mx=max(mx,s[i]);
    }
    s[n]=0;
	build(n+1,mx+1);
    getHeight(s,n);
    int l=0,r=n,ans;
   // for(int i=0;i<=n;i++){
    //	cout<<sa.sa[i];
//	} 
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(n,mid)) l=mid+1,ans=mid;
        else r=mid-1;
    }
    int pos=0;
    int cnt=1;
    for(int i=2;i<=n;i++){
        if(height[i]>=ans)
        {
            cnt++;
			pos=i;
            if(cnt>=2){
                break;
            }
        }
        else {
            cnt=1;
        }
    }
    pos=sa[pos];
    for(int i=pos;i<pos+ans;i++)
		printf("%c",S[i]);
    return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值