LA 4513 Stammering Aliens

Description

Download as PDF 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 ofs that appears at least m times. For example, in the messagebaaaababababbababbab, the length-5 wordbabab is contained 3 times, namely at positions5, 7 and 12 (where indices start at zero). No substring appearing3 or more times is longer (see the first example from the sample input). On the other hand, no substring appears11 times or more (see example 2).

In case there are several solutions, the substring with the rightmost occurrence is preferred (see example3).

Input

The input contains several test cases. Each test case consists of a line with an integer m ( m$ \ge$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.

Output

Print 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 possible starting position of such a substring.

Sample Input

3
baaaababababbababbab
11
baaaababababbababbab
3
cccccc
0

Sample Output

5 12
none
4 2

Source


解析

为每个后缀弄个H值(H[i]=H[i+1]*x+s[i]),对于串s[4]=abcd
H[4]=0
H[3]=s[3]                             d
H[2]=s[3]*x  +s[2]                   cd
H[1]=s[3]*x^2+s[2]*x  +s[1]         bcd
H[0]=s[3]*x^3+s[2]*x^2+s[1]*x+s[0] abcd
对于一段长L的子串s[k,k+L)的hash值是H[k]-H[k+L]*x^L
展开:
s[k+L-1]*x^(L-1)+s[k+L-2]*x^(L-2)+...+s[k+1]*x+s[k]

用hash实现最长公共前缀……注意hash的取值。开始用random生成hash,wa了好多发……

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<cstdlib>
using namespace std;
char s[40100];
unsigned long long H[40100],hash[40100],xp[40100],x;
int N,rank[40100],pos,M;

void prework()
{
	scanf("%s",s); N=strlen(s);
	//find H
	H[N]=0;
	for(int i=N-1;i>=0;i--) H[i]=H[i+1]*x+(s[i]-97);
}

int cmp(int a,int b)
{return hash[a]<hash[b] || (hash[a]==hash[b] && a<b);}
bool Check(int x)
{
	for(int i=0;i<=N-x;i++) rank[i]=i,hash[i]=H[i]-H[i+x]*xp[x];
	sort(rank,rank+N-x+1,cmp);

	int sum=0; pos=-1;
	for(int i=0;i<=N-x;i++)
	{
		if(i>0&&hash[rank[i]]!=hash[rank[i-1]]) sum=0;
		if(++sum>=M) pos=max(pos,rank[i]);
	}
	return pos>=0;
}

void Binary_Search()
{
	int L=1,R=N+1,mid;//[l,r)
	while(L<R)
	{
		mid=(L+R)>>1;
		if(Check(mid)) L=mid+1;
		else R=mid;
	}
	
	if(--L) {Check(L);printf("%d %d\n",L,pos);}
	else puts("none");
}

int main()
{
	srand(time(NULL));
	x=(int)((double)rand()/(RAND_MAX+1)*100)+100;
	x=31;
	//find x^L
	xp[0]=1;
	for(int i=1;i<=40005;i++) xp[i]=xp[i-1]*x;
	while(scanf("%d",&M)==1 && M)
	{
		prework();
		Binary_Search();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值