HDU4080Stammering Aliens(后缀数组+二分)

However, all efforts to decode their messages have failed so far because, as luck would have it, they have stumbled upon a race of stuttering aliens! Her team has found out that, in every long enough message, the most important words appear repeated a certain number of times 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 has 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 is to find the longest substring of s that appears at least m times. For example, in the message baaaababababbababbab, the length-5 word babab is contained 3 times, namely at positions 5, 7 and 12 (where indices start at zero). No substring appearing 3 or more times is longer (see the first example from the sample input). On the other hand, no substring appears 11 times or more (see example 2). In case there are several solutions, the substring with the rightmost occurrence is 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 containing a string s of length between m and 40 000, inclusive. All characters in s are lowercase characters from "a" to "z". The last test case is denoted by m = 0 and must not be processed. 
OutputPrint one line of output for each test case. If there is no solution, output none; otherwise, print two integers in a line, 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

3
baaaababababbababbab
11
baaaababababbababbab
3
cccccc
0

Sample Output

5 12
none
4 2

题意:

求字符串里面出现的子序列中出现超过m次的最长子序列(可交叉),没有输出none,有则输出长度,和最后一次出现的位置。

思路:

求出后缀数组,二分长度,得到长度,至于最后一次的位置,需要把每个位置枚举一下,即大于Mid的ht[i],对应着的sa[i],和sa[i-1]。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
const int maxn=;
char ch[maxn]; int m;
int max(int x,int y){if(x>y) return x;return y;}
struct SA
{
int Rank[maxn],sa[maxn],tsa[maxn],cntA[maxn],cntB[maxn],A[maxn],B[maxn];
int ht[maxn],N;
void get_sa()
{
N=strlen(ch+);
for(int i=;i<=;i++) cntA[i]=;
for(int i=;i<=N;i++) cntA[ch[i]]++;
for(int i=;i<=;i++) cntA[i]+=cntA[i-];
for(int i=N;i>=;i--) sa[cntA[ch[i]]--]=i;
Rank[sa[]]=;
for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(ch[sa[i]]==ch[sa[i-]]?:);
for(int l=;Rank[sa[N]]<N;l<<=){
for(int i=;i<=N;i++) cntA[i]=cntB[i]=;
for(int i=;i<=N;i++) cntA[A[i]=Rank[i]]++;
for(int i=;i<=N;i++) cntB[B[i]=i+l<=N?Rank[i+l]:]++;
for(int i=;i<=N;i++) cntA[i]+=cntA[i-],cntB[i]+=cntB[i-];
for(int i=N;i>=;i--) tsa[cntB[B[i]]--]=i;
for(int i=N;i>=;i--) sa[cntA[A[tsa[i]]]--]=tsa[i];
Rank[sa[]]=;
for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(A[sa[i]]==A[sa[i-]]&&B[sa[i]]==B[sa[i-]]?:);
}
}
void get_ht()
{
for(int i=,j=;i<=N;i++){
if(j) j--;
while(ch[i+j]==ch[sa[Rank[i]-]+j]) j++;
ht[Rank[i]]=j;
}
}
int check(int L)
{
int cnt=,tmppos=,pos=;
for(int i=;i<=N;i++){
if(ht[i]<L) cnt=,tmppos=;
else cnt++,tmppos=max(tmppos,max(sa[i],sa[i-]));
if(cnt>=m) pos=max(tmppos,pos);
} return pos;
}
void solve()
{
//for(int i=1;i<=N;i++) printf("%d ",sa[i]);printf("\n");
//for(int i=1;i<=N;i++) printf("%d ",ht[i]);printf("\n");
int L=,R=N,ans=,tmp,anspos=;
while(L<=R){
int Mid=(L+R)>>;
tmp=check(Mid);
if(tmp) {
ans=Mid;anspos=tmp; L=Mid+;
} else R=Mid-;
}
if(ans==) printf("none\n");
else printf("%d %d\n",ans,anspos-);
}
}Sa;
int main()
{
int i,j,k;
while(~scanf("%d",&m)&&m){
scanf("%s",ch+);
if(m==){
printf("%d 0\n",strlen(ch+));
continue;
}
Sa.get_sa();
Sa.get_ht();
Sa.solve();
} return ;
}
上一篇:POJ1226 Substrings(二分+后缀数组)


下一篇:UVALive - 4513 Stammering Aliens ——(hash+二分 || 后缀数组加二分)