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
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<
//	} 
    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;
}


你可能感兴趣的:(acm竞赛,二分法)