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

來源:互聯網
上載者:User

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 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 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 integerm (m1), 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 
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;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.