LA 4513(Stammering Aliens-Hash求LCP)[Template:hash求LCP]

本文介绍了一种通过Hash算法快速查找重复子串的方法,旨在帮助解析来自外星文明的重复信号模式。面对复杂的外星信息,算法需找出至少重复指定次数的最长子串,以揭示信息中的关键部分。

4513 - 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 messagebabab, which has been abbreviated because the secondb of the first word can be reused as the firstb 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 integerm (m$ \ge$1), the minimum number of repetitions, followed by a line containing a string s of length betweenm and 40 000, inclusive. All characters ins are lowercase characters from ``a'' to ``z''. The last test case is denoted bym = 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 leastm 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


本题为lrj的白书中Hash求LCP入门题。

方法请参考白书


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (40000+10)
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}

int n,m;
char s[MAXN];
const int x=143;
ull H[MAXN],xp[MAXN],hash[MAXN];
int rank[MAXN];

int cmp(const int &a,const int &b)
{
	return hash[a]<hash[b]||(hash[a]==hash[b]&&a<b);
}

int is_ok(int M)
{
	int pos=-1,c=0;
	
	Rep(i,n-M+1)                                                                                             
	{
		rank[i]=i;hash[i]=H[i]-H[i+M]*xp[M];
	}
	
	sort(rank,rank+1+n-M,cmp); //排序 rank[0]--rank[n-m] 
	
	Rep(i,n-M+1)
	{
		if (i==0||hash[rank[i]]^hash[rank[i-1]]) c=0; //是否有以前的‘积累’ 
		c++;
		if (c>=m) pos=max(pos,rank[i]);
	}	
	return pos;
}

int main()
{
//	freopen("la4513.in","r",stdin);
//	freopen(".out","w",stdout);
	
	
	
	xp[0]=1;
	For(i,MAXN-1) xp[i]=xp[i-1]*x;
	
	while(scanf("%d%s",&m,s)!=EOF&&m>0)
	{
		n=strlen(s);
		H[n]=0;
		RepD(i,n-1) H[i]=H[i+1]*x+s[i]-'a';
				
		if (is_ok(1)==-1) printf("none\n");
		else
		{
			int L=1,R=n,ans=m;
			while (L<=R)
			{
				int M=(L+R)>>1;
				if (is_ok(M)!=-1) L=M+1,ans=M;
				else R=M-1;		
			}
			printf("%d %d\n",ans,is_ok(ans));
		} 
		
	}
	
	
	
	return 0;
}




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值