2015
04-16

# Stammering Aliens

Dr. Ellie Arroway has established contact with an extraterrestrial civilization. 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).

The 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.

The 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.

3
baaaababababbababbab
11
baaaababababbababbab
3
cccccc
0

5 12
none
4 2

/*

*/

#include<stdio.h>
#include<string.h>
const int maxn=500010;

int wa[maxn],wb[maxn],wv[maxn],ws[maxn];
int sa[maxn],ss[maxn];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)//r[0  n-1]   sa[0    n-1]   最大值小于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]]++;//x==rank   难道是统计字符个数
for(i=1; i<m; i++)
ws[i]+=ws[i-1];//加上前一个
for(i=n-1; i>=0; i--)
sa[--ws[x[i]]]=i;//排第[i]的是谁

for(j=1,p=1; p<n; j*=2,m=p)
{
for(p=0,i=n-j; i<n; i++) //变量j是当前字符串的长度，
y[p++]=i;// 数组y保存的是对第二关键字排序的结果
for(i=0; i<n; i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;

for(i=0; i<n; i++)
wv[i]=x[y[i]];
for(i=0; i<m; i++)
ws[i]=0;
for(i=0; i<n; i++)
ws[wv[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++;
}
return;
}

int rank[maxn],height[maxn];
void calheight(int *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++)
;
return;
}

int pos;
int po;
int ok2(int chang,int ci,int len)
{
po=-1;
int zui=-1,f=0;
int i,jishu=0;
for(i=2;i<=len;++i)
{
if(height[i]>=chang)
{
jishu++;
if(sa[i-1]>=zui) zui=sa[i-1];
if(sa[i]>zui) zui=sa[i];
if(jishu>=ci){ f=1;//少了这个括号
if(zui>po) po=zui;}
}else
{
jishu=0;
zui=-1;
}
}
if(f)
{
return 1;
}else{
return 0;
}
}

bool ok(int x, int m,int n) {
int i, flag = 0;
po = -1;
int tm = 0, tt = -1;
for (i=2; i<=n; i++) {
if (height[i] >= x) {
tm++;
if (sa[i-1] > tt) tt = sa[i-1];
if (sa[i] > tt) tt = sa[i];
if (tm >= m) {
flag = 1;
if (tt > po)
po = tt;
}
}
else {
tm = 0;
tt = -1;
}
}
if (flag)
return true;
return false;
}
int  main()
{
int m;
while(scanf("%d",&m),m)
{
getchar();
char r[maxn];

gets(r);
int i;
int len=strlen(r);
if (m == 1) {//少了这个
printf("%d %d\n", len, 0);
continue;
}
for(i=0; i<len; ++i)
{
ss[i]=r[i];
}
ss[len]=0;
da(ss,sa,len+1,128);

calheight(ss,sa,len);//为什么是len？？？

int ret =0,hi=len-m+1,low=1,mid,pp;
po=0;
pos=0;
while(hi>=low)
{
mid=(hi+low)/2;
if(ok2(mid,m-1,len))//
{
pp=po;
ret=mid;
low=mid+1;
}else
{
hi=mid-1;
}
}
if(ret)
{
printf("%d %d\n",ret,pp);
}else
{
printf("none\n");
}
}
return 0;
}


1. 这道题目的核心一句话是：取还是不取。
如果当前取，则index+1作为参数。如果当前不取，则任用index作为参数。

2. 在方法1里面：

//遍历所有的边，计算入度
for(int i=0; i<V; i++)
{
degree = 0;