Stammering Aliens (Hash+二分)

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). Input 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. 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

#include <stdio.h>
#include <string.h> 
#include <map>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
char str[100010];
int q; 
#define p 233
map<ull,int>ma;
ull Hash[100010],a[100010];
int n,len;
struct node{
	int ha,id;
}qwe[100010]; 
bool cmp(node a,node b)
{
	if(a.ha==b.ha) return a.id<b.id;//id大的放在后面有助于找到最后面的起始位置 
	
	else return a.ha<b.ha;
}
int init(int len)//初始化hash 
{
	Hash[0]=0;
	for(int i=1;i<=len;i++)
	{
		Hash[i]=Hash[i-1]*p+str[i];
	}
	a[0]=1;
	for(int i=1;i<100010;i++)
	{
		a[i]=a[i-1]*p;
	}
}
int judge(int l)
{
	 
	int ans=-1,i,j;
	qwe[0].ha=Hash[0];qwe[0].id=0;
	for(  i=1;i<=len-l+1;i++)
	{
		 qwe[i].ha=Hash[i+l-1]-Hash[i-1]*a[l];
		 qwe[i].id=i;
	}
	sort(qwe,qwe+len-l+2,cmp);
	for(  i=0;i<=len-l+1;i++)
	{
		int v=0;
		for(  j=i;qwe[i].ha==qwe[j].ha&&j<=len-l+1;j++)
		{
			v++;
			if(v>=n)
			{
				ans=max(ans,qwe[j].id-1);
			}
		}
		i=j-1;
	}
	return  ans;
	
}
	
}
int main()
{
	while(scanf("%d",&n)&&n)
	{
	 
		scanf("%s",str+1);
		 len=strlen(str+1);
		init(len);
		int l=0,r=len+1;
		while(l<=r)//二分 
		{
			int mid=(l+r)/2;
			if(judge(mid)!=-1)
			{
				l=mid+1;
		 
			}
			else r=mid-1;
		}
	 
	 	if(judge(1)==-1) puts("none"); //注意这个判断条件 
		else printf("%d %d\n",r,judge(r)) ;
		 
		
	}
	return 0;
	
}

2

猜你喜欢

转载自blog.csdn.net/qq_42434171/article/details/81812970