CS Academy Round #49 C.Max Substring 【后缀数组】

来源:互联网 发布:2017淘宝怎么提高销量 编辑:程序博客网 时间:2024/04/30 15:27


MaxSubstring

Time limit: 1000 ms
Memory limit: 256 MB


You are given a string S.Find a string T thathas the most number of occurrences as a substring in S.

If the solution is notunique, you should find the one with maximum length. If the solution is stillnot unique, find the smallest lexicographical one.

Standard input

The first line contains string S.

Standard output

Print string T onthe first line.

Constraints and notes

  • S consists of lowercase letters of the English alphabet
  • The length of S is between 1 and 105 

Input

Output 

Explanation

cabdab
ab

ab and ab appear 2 times, but ab is bigger. There're no other substrings that appear more than once.

cabcabc
c

c appears 3 times while abab,cabcabc and cab appear only 2 times.

ababababab
ab

Note that we're interested in substrings(continuous) not subsequences.

ab is the winner with 5 appearances.

 


【题意】


给出一个字符串,求在其中出现次数最多的子串。


【思路】


后缀数组模板题。


我们先求出height数组记录排名相邻的两个子串的最长公共前缀,那么只要最长公共前缀大于等于1便是某一个子串的出现次数加1。


那么我们只要更新下某个子串出现的次数,当次数大于此前值的时候更新。


由于当次数相同时,输出长度长的,那么当次数相等且长度更长时也更新。


更新时注意保存当前子串的初始位置sa[i]。便于最后的输出。


当一次也没更新时,说明所有子串都只出现了一次,那么输出最长的(原串)。


#include <cstdio>#include <cmath>#include <queue>#include <vector>#include <iostream>#include <map>#include <cstring>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define rush() int T;scanf("%d",&T);while(T--)typedef long long ll;const int maxn = 100005;const ll mod = 1e9+7;const int INF = 0x3f3f3f;const double eps = 1e-9;int t1[maxn],t2[maxn],c[maxn];bool cmp(int *r,int a,int b,int l){    return r[a]==r[b]&&r[a+l]==r[b+l];}void da(int *str,int *sa,int *Rank,int *height,int n,int m){    n++;    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]=str[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]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;        }        if(p>=n) break;        m=p;    }    int k=0;    n--;    for(i=0;i<=n;i++) Rank[sa[i]]=i;    for(i=0;i<n;i++)    {        if(k) k--;        j=sa[Rank[i]-1];        while(str[i+k]==str[j+k]) k++;        height[Rank[i]]=k;    }}int Rank[maxn],height[maxn],sa[maxn];char s[maxn];int r[maxn];int main(){    scanf("%s",s);    int n=strlen(s);    for(int i=0;i<n;i++)    {        r[i]=s[i]-'a'+1;    }    r[n]=0;    da(r,sa,Rank,height,n,30);    int ans=0,anslen=0;    int cnt=0,len=INF;    int pos=-1;    for(int i=0;i<=n;i++)    {        if(height[i])        {            cnt++;            len=min(len,height[i]);            if(cnt>ans)            {                ans=cnt;                anslen=len;                pos=sa[i];            }            else if(cnt==ans&&len>anslen)            {                anslen=len;                pos=sa[i];            }        }        else        {            cnt=0;            len=INF;        }    }    if(pos==-1)    {        printf("%s\n",s);        return 0;    }    for(int i=pos;i<min(n,pos+anslen);i++)    {        printf("%c",s[i]);    }    puts("");}