Hdu 2459 suffix Array to find the maximum number of repetitions substrings

Source: Internet
Author: User
#include <cmath> #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int n=100005;
int ws[n],wv[n],wa[n],wb[n];
int rank[n],height[n],sa[n],l;
Char S[n],xiao;
int dp[n][25];
int cmp (int *r,int a,int b,int l) {return r[a]==r[b]&&r[a+l]==r[b+l];} void da (char *r,int *sa,int n,int m)
    {int i,j,p,*x=wa,*y=wb,*t;
    for (i=0;i<m;i++) ws[i]=0;
    for (i=0;i<n;i++) ws[x[i]=r[i]]++;
    for (i=1;i<m;i++) ws[i]+=ws[i-1];
    for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
        for (j=1,p=1;p<n;j*=2,m=p) {for (p=0,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++) ws[i]=0;
        for (i=0;i<n;i++) ws[wv[i]=x[y[i]]]++;
        for (i=1;i<m;i++) ws[i]+=ws[i-1];
        for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
    for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++) x[sa[i]]=cmp (y,sa[i-1],sa[i],j)? p-1:p++; }} void Calheight (chaR *r,int *sa,int N) {int i,j,k=0;
    for (i=1;i<=n;i++) rank[sa[i]]=i;
for (i=0;i<n;height[rank[i++]]=k) for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
    } void RMQ ()//rmq initialize {int i,j,m;
    m= (int) (log (double) l)/log (2.00));
    for (i=1;i<=l;i++) dp[i][0]=height[i]; for (j=1;j<=m;j++) for (i=1;i+ (1<<j) -1<=l;i++) dp[i][j]=min (dp[i][j-1],dp[i+ (1<< (j-1))
][j-1]);
    } int LCP (int x,int y)//To find the longest public prefix {int t;
    X=rank[x];y=rank[y];
    if (x>y) swap (x, y);
    x + +;
    t= (int) (log (double (y-x+1))/log (2.00));
return min (dp[x][t],dp[y-(1<<t) +1][t]);
    } void Solve ()//To find the maximum number of repetitions of the substring {int i,j,max=1,f=0,l1,num=0,t,node=1,k,cnt,p; for (i=;i<=l/2;i++)//I&LT;=LEN/2 optimized, enumeration of different lengths of the loop section {for (j=0;j+i<l;j+=i)//enumeration substring start {if (S[j]
           !=s[j+i])//pruning continue;
       L1=LCP (j,j+i);//find the longest common prefix of J position and J+i position num=l1/i+1;//j...j+i-1 for the follow-up p=j;//start    t=i-l1%i;//because L1 is not necessarily an integer multiple of the length of the loop string, and we previously enumerated J at the time of each increment I may miss cnt=0;//some may become the beginning of the string, the number of repetitions of more nodes, T recorded in the J before the occurrence of a number of equal characters can be reconstructed
           Loops are different, but the number of cycles is num+1 for (k=j-1;k>=0&&k+i>j&&s[k]==s[k+i];k--)//The process of looking for more repetitions or rank-value strings
               {cnt++;
                   if (cnt==t) {num++;
               P=k;
           } else if (rank[k]<rank[p]) p=k;
               } if (max<num) {f=p;
               Max=num;
           Node=i;
               } else if (Max==num&&rank[f]>rank[p]) {f=p;
           Node=i;
        }}} if (Max==1) {printf ("%c\n", Xiao);
    return;
    } for (i=f;i<=f+max*node-1;i++) printf ("%c", S[i]);
printf ("\ n");
    } int main () {int kase=0,i;
        while (scanf ("%s", s)!=eof&&s[0]!= ' # ') {L=strlen (s); XIao= ' z ' +1;
        for (i=0;i<l;i++) if (S[i]<xiao) xiao=s[i];
        s[l]= ' 0 ';
        Da (s,sa,l+1, ' z ' + 1);
        Calheight (s,sa,l);
        RMQ ();
        printf ("Case%d:", ++kase);
    Solve ();
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.