hdu4080stammering Aliens (suffix array + dichotomy)

Source: Internet
Author: User

However, all efforts to decode their messages has failed so far because, as luck would has it, they has stumbled upon a Race of Stuttering aliens! Her team had found out that, in every long enough message, the most important words appear repeated a certain number of TI Mes as a sequence of consecutive characters, even in the middle of other words. Furthermore, sometimes they use contractions in an obscure manner. For example, if they need to say Bab twice, they might just send the message babab, which have been abbreviated because the Second B of the first word can be reused as the first B of the second one.
Thus, the message contains possibly overlapping repetitions of the same words over and over again. As a result, Ellie turns to you, S.R. Hadden, for help in identifying the gist of the message.
Given an integer m, and a string s, representing the message, your task was to find the longest substring of s that appears At least m times. For example, in the message baaaababababbababbab, the length-5 word babab was contained 3 times, namely at positions 5, 7 a nd (where indices start at zero). No substring appearing 3 or more times are longer (see the first example from the sample input). On the other hand, no substring appears one times or more (see Example 2). In case there was several solutions, the substring with the rightmost occurrence was preferred (see Example 3).

Inputthe input contains several test cases. Each test case consists of a line with an integer m (M >= 1), the minimum number of repetitions, followed by a line con Taining a string s of length between m and inclusive. All characters in S is lowercase characters from "a" to "Z". The last test case was denoted by M = 0 and must not being processed.
Outputprint one line of output for each test case. If There is no solution, output none; Otherwise, print, integers in a, separated by a space. The first integer denotes the maximum length of a substring appearing at least m times; The second integer gives the rightmost starting position of this substring.
Sample Input

3baaaababababbababbab11baaaababababbababbab3cccccc0

Sample Output

5 12none4 2
Test instructions

Find the oldest sequence in a subsequence that appears in a string that exceeds m (can be crossed), without output none, with the output length, and the last occurrence.

Ideas:

Find the suffix array, binary length, get the length, as for the last position, need to enumerate each position, that is greater than mid ht[i], corresponding to the sa[i], and sa[i-1].

#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#include<string>#include<cmath>using namespacestd;Const intmaxn=40010;CharCH[MAXN];intm;intMaxintXintY) {if(x>y)returnXreturny;}structsa{intRANK[MAXN],SA[MAXN],TSA[MAXN],CNTA[MAXN],CNTB[MAXN],A[MAXN],B[MAXN]; intHt[maxn],n; voidGet_sa () {N=strlen (ch+1);  for(intI=0; i<= -; i++) cnta[i]=0;  for(intI=1; i<=n;i++) cnta[ch[i]]++;  for(intI=1; i<= -; i++) cnta[i]+=cnta[i-1];  for(inti=n;i>=1; i--) sa[cnta[ch[i]]--]=i; rank[sa[1]]=1;  for(intI=2; i<=n;i++) rank[sa[i]]=rank[sa[i-1]]+ (ch[sa[i]]==ch[sa[i-1]]?0:1);  for(intL=1; rank[sa[n]]<n;l<<=1){             for(intI=1; i<=n;i++) cnta[i]=cntb[i]=0;  for(intI=1; i<=n;i++) cnta[a[i]=rank[i]]++;  for(intI=1; i<=n;i++) cntb[b[i]=i+l<=n? RANK[I+L]:0]++;  for(intI=1; i<=n;i++) cnta[i]+=cnta[i-1],cntb[i]+=cntb[i-1];  for(inti=n;i>=1; i--) tsa[cntb[b[i]]--]=i;  for(inti=n;i>=1; i--) sa[cnta[a[tsa[i]]]--]=Tsa[i]; rank[sa[1]]=1;  for(intI=2; i<=n;i++) rank[sa[i]]=rank[sa[i-1]]+ (a[sa[i]]==a[sa[i-1]]&&b[sa[i]]==b[sa[i-1]]?0:1); }    }    voidget_ht () { for(intI=1, j=0; i<=n;i++){            if(j) j--;  while(ch[i+j]==ch[sa[rank[i]-1]+J]) J + +; Ht[rank[i]]=J; }    }    intCheckintL) {intCnt=1, tmppos=0, pos=0;  for(intI=1; i<=n;i++){            if(ht[i]<l) cnt=1, tmppos=0; ElseCnt++,tmppos=max (Tmppos,max (sa[i],sa[i-1])); if(cnt>=m) pos=Max (Tmppos,pos); }  returnPOS; }    voidsolve () {//for (int i=1;i<=n;i++) printf ("%d", Sa[i]);p rintf ("\ n"); //for (int i=1;i<=n;i++) printf ("%d", Ht[i]);p rintf ("\ n");        intL=0, r=n,ans=0, tmp,anspos=0;  while(l<=s) {            intMid= (l+r) >>1; TMP=check (Mid); if(TMP) {ans=mid;anspos=tmp; L=mid+1; } Elser=mid-1; }        if(ans==0) printf ("none\n"); Elseprintf"%d%d\n", ans,anspos-1); }}sa;intMain () {inti,j,k;  while(~SCANF ("%d", &m) &&m) {scanf ("%s", ch+1); if(m==1) {printf ("%d 0\n", strlen (ch+1)); Continue;         } Sa.get_sa ();         Sa.get_ht ();    Sa.solve (); } return 0;}

hdu4080stammering Aliens (suffix array + dichotomy)

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.