HDOJ 4080 Stammering Aliens
来源:互联网 发布:美工岗位说明书 编辑:程序博客网 时间:2024/06/08 11:08
又用后缀数组做了一遍。。。。
c要开很大才能过。。
Stammering Aliens
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 597 Accepted Submission(s): 224
Problem Description
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).
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 starting position of this substring.
Sample Input
3baaaababababbababbab11baaaababababbababbab3cccccc0
Sample Output
5 12none4 2
Source
2009 South Western European Regional Contest
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=40010;int sa[maxn],rank[maxn],rank2[maxn],h[maxn],c[maxn],*x,*y,m;char str[maxn];void INIT(){ memset(sa,0,sizeof(sa)); memset(rank,0,sizeof(rank)); memset(rank2,0,sizeof(rank2)); memset(h,0,sizeof(h)); memset(c,0,sizeof(c));}bool cmp(int* r,int a,int b,int l,int n){ if(r[a]==r[b]&&a+l<n&&b+l<n&&r[a+l]==r[b+l]) return true; return false;}void radix_sort(int n,int sz){ for(int i=0;i<sz;i++) c[i]=0; for(int i=0;i<n;i++) c[x[y[i]]]++; for(int i=1;i<sz;i++) c[i]+=c[i-1]; for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];}void get_sa(char c[],int n,int sz=128){ x=rank,y=rank2; for(int i=0;i<n;i++) x[i]=c[i],y[i]=i; radix_sort(n,sz); for(int len=1;len<n;len<<=1) { int yid=0; for(int i=n-len;i<n;i++) y[yid++]=i; for(int i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len; radix_sort(n,sz); swap(x,y); x[sa[0]]=yid=0; for(int i=1;i<n;i++) { x[sa[i]]=cmp(y,sa[i],sa[i-1],len,n)?yid:++yid; } sz=yid+1; if(sz>=n) break; } for(int i=0;i<n;i++) rank[i]=x[i];}void get_h(char str[],int n){ int k=0; h[0]=0x3f3f3f3f; for(int i=0;i<n;i++) { if(rank[i]==0) continue; k=max(k-1,0); int j=sa[rank[i]-1]; while(i+k<n&&j+k<n&&str[i+k]==str[j+k]) k++; h[rank[i]]=k; }}bool ck(int L,int n){ int len=0,temp=1; for(int i=1;i<=n;i++) { if(h[i]>=L) temp++; else { len=max(len,temp);temp=1; } } if(len>=m) return true; return false;}int doit(int L,int n){ int ret=-1,temp=1,tt=sa[0]; for(int i=1;i<=n;i++) { if(h[i]>=L) { temp++; tt=max(sa[i],tt); } else { if(temp>=m) { ret=max(ret,tt); } temp=1;tt=sa[i]; } } return ret;}int main(){while(scanf("%d",&m)!=EOF&&m){ INIT(); scanf("%s",str); int n=strlen(str); get_sa(str,n); get_h(str,n); int low=1,high=n,mid,lenglengago=-1; while(low<=high) { mid=(low+high)/2; if(ck(mid,n)) lenglengago=mid,low=mid+1; else high=mid-1; } if(lenglengago==-1) puts("none"); else { printf("%d %d\n",lenglengago,(m==1)?0:doit(lenglengago,n)); }} return 0;}
0 0
- HDOJ 4080 Stammering Aliens
- HDOJ 4080 Stammering Aliens
- 【字符串哈希】 HDOJ 4080 Stammering Aliens
- HDU 4080 Stammering Aliens
- hdu 4080 Stammering Aliens - 后缀数组
- HDU 4080 Stammering Aliens(后缀数组)
- HDU 4080 Stammering Aliens && 后缀数组
- hdu 4080 Stammering Aliens 二分 hash
- [后缀数组+二分] hdu 4080 Stammering Aliens
- zoj 3395 Stammering Aliens
- LA 4513 Stammering Aliens
- LA4513 Stammering Aliens
- LA 4513 - Stammering Aliens
- LA 4513 Stammering Aliens
- LA 4513 Stammering Aliens
- LA 4513 Stammering Aliens
- UVALive - 4513 Stammering Aliens
- Stammering Aliens 【二分+哈希】
- (转)oracle入门心得----强烈推荐
- 设计模式深入学习IOS版(1)基础篇之类关系
- 漫谈 Clustering (番外篇): Vector Quantization
- html转义字符本文
- java 排序 ArrayList sort方法
- HDOJ 4080 Stammering Aliens
- 实达打印机-BP3000XE设置参数
- xxx
- FLEX发布问题,流错误2032,找不到swz文件
- Memcache问题集锦
- javascript中新建类,对象方法总结
- AT&T与蓝色巨人IBM牵手,试水大数据驱动公共服务
- Android共有五种布局方式
- Big Endian与Little Endian区别