UVALive4513 Stammering Aliens(哈希法,后缀数组)
2015-12-10 17:36
525 查看
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12580
【思路】
求出现次数不小于k次的最长可重叠子串和最后的出现位置。
法一:
后缀数组,二分长度,划分height。时间复杂度为O(nlogn)
法二:
Hash法。构造字符串的hash函数,二分长度,求出hash(i,L)后排序,判断是否存在超过k个相同hash 值得块即可。时间为O(nlog2n).
法三:(UPD.16/4/6)
SAM。求|right|。
注意划分height一定要精确且如果m=1需要特判
【代码1】
hash
【思路】
求出现次数不小于k次的最长可重叠子串和最后的出现位置。
法一:
后缀数组,二分长度,划分height。时间复杂度为O(nlogn)
法二:
Hash法。构造字符串的hash函数,二分长度,求出hash(i,L)后排序,判断是否存在超过k个相同hash 值得块即可。时间为O(nlog2n).
法三:(UPD.16/4/6)
SAM。求|right|。
注意划分height一定要精确且如果m=1需要特判
【代码1】
//1628ms #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef unsigned long long ULL; const int maxn = 80000+10; const int x = 233; ULL hash[maxn],xp[maxn],H[maxn]; int m,n; char s[maxn]; int cmp(const int& a,const int& b) { return hash[a]<hash[b] || (hash[a]==hash[b] && a<b); } int pos,rank[maxn]; bool can(int L) { pos=-1; for(int i=0;i<n-L+1;i++) hash[i]=H[i]-H[i+L]*xp[L],rank[i]=i; sort(rank,rank+n-L+1,cmp); int cnt=0; for(int i=0;i<n-L+1;i++) { if(!i || hash[rank[i]]!=hash[rank[i-1]]) cnt=0; if(++cnt>=m) pos=max(pos,rank[i]); } return pos>=0; } int main() { //freopen("in.in","r",stdin); //freopen("outr.out","w",stdout); while(scanf("%d",&m)==1 && m) { scanf("%s",s); n=strlen(s); H =0,xp[0]=1; for(int i=n-1;i>=0;i--) H[i]=H[i+1]*x+s[i]-'a'; for(int i=1;i<=n;i++) xp[i]=xp[i-1]*x; if(!can(1)) printf("none\n"); else { int L=1,R=n+1; while(L<R) { int M=L+(R-L+1)/2; if(can(M)) L=M; else R=M-1; } can(L); printf("%d %d\n",L,pos); } } return 0; }
hash
相关文章推荐
- JS中运算符“||”和“&&”的总结
- 苹果App Store 2015年度最佳应用游戏公布
- 苹果App Store 2015年度最佳应用游戏公布
- object-c和Java的简单比较
- 04.iOS 使用lame将wav转换为mp3
- docker本地镜像仓库搭建
- 《HTTP权威指南》– 10.安全HTTP
- Java实现DES加密解密代码
- iOS重新定义系统自带的方法,如 重定义 CGRectMake 和 CGPointMake 可以解决屏幕适配的问题
- EventBus源码解析
- python argparse 模板
- ZK listbox 两种分页使用及比较
- 无法解析具体reference那个同名文件
- 开发环境配置--Ubuntu+Qt4+OpenCV(三)
- Ghost博客安装
- 老李分享:使用 Python 的 Socket 模块开发 UDP 扫描工具3
- 继承ActionBarActivity,被画横线,过时为题
- [Javascript] JSONP(未完成)
- centos6系统优化脚本
- C#窗体应用程序实现cocos2dx json文件自动生成lua文件