LA 4513(Stammering Aliens-Hash求LCP)[Template:hash求LCP],4513lcp
4513 - Stammering AliensDr. 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 leastm times. For example, in the messagebaaaababababbababbab, the length-5 wordbabab is contained3 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 (
m1), 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
3baaaababababbababbab11baaaababababbababbab3cccccc0
Sample Output
5 12none4 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;}