hash(二分+hash)使用map会tle
来源:互联网 发布:钓鱼生成软件下载 编辑:程序博客网 时间:2024/06/05 02:58
Stammering Aliens
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1582 Accepted Submission(s): 527
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慎用map代码:#include<bits/stdc++.h>using namespace std;typedef long long ll;const int N=40010,P=31,D=1000173169;int poww[N],f[N],s,m;char a[N];struct node{ int x,y;} q[N];bool cmp(node a,node b){ if(a.x!=b.x) return a.x<b.x; return a.y<b.y;}int hashh(int l,int r){ return (ll)(f[r]-(ll)f[l-1]*poww[r-l+1]%D+D)%D;}int check(int x){ int flag=0,sum=0; for(int i=1; i<=s-x+1; i++) { q[i].x=hashh(i,i+x-1); q[i].y=i; } sort(q+1,q+s-x+2,cmp); for(int i=1; i<=s-x+1; i++) { if(q[i].x==q[i-1].x) sum++; else { sum=1; } if(sum>=m) { flag=1; break; } } if(flag) return 1; return 0;}int maxx(int x,int y){ if(x>y) return x; return y;}int checkk(int x){ int biao=0,sum=0; for(int i=1; i<=s-x+1; i++) { q[i].x=hashh(i,i+x-1); q[i].y=i; } sort(q+1,q+s-x+2,cmp); for(int i=1; i<=s-x+1; i++) { if(q[i].x==q[i-1].x) sum++; else { sum=1; } if(sum>=m) { biao=maxx(biao,q[i].y); } } return biao;}int main(){ while(scanf("%d",&m)!=EOF) { if(!m) return 0; scanf("%s",a+1); s=strlen(a+1);// printf("%d\n",s); int i; for(poww[0]=i=1; i<=s; i++) poww[i]=(ll)poww[i-1]*P%D; for(i=1; i<=s; i++ ) f[i]=(ll)((ll)f[i-1]*P+a[i])%D; int l=0,r=s; while(l<r) { int mid=(l+r+1)>>1; if(check(mid)) { l=mid; } else r=mid-1; }// printf("%d\n",check(4)); if(l==0) printf("none\n"); else { printf("%d %d\n",l,checkk(l)-1); } }}/*3baaaababababbababbab1baaaababababbababbab3cccccc0*///#include<bits/stdc++.h>//using namespace std;//int main()//{////}
阅读全文
0 0
- hash(二分+hash)使用map会tle
- hdu 5199 (二分||Hash||map)
- hash map的使用
- Hash Map / Hash Set
- 矩阵hash+二分(慎用map)
- gcc 使用hash—map
- hash map
- 【二分+map或Hash】bzoj 1567 战役地图
- POJ 2503 Babelfish(map或hash或排序二分)
- hash 使用
- ural 1613【hash+二分】
- hdu_1496_equations( 二分 or hash )
- BZOJ2258【hash】【二分】
- bzoj 1692 二分+hash
- 字符串hash + 二分答案
- hdu6138 hash+二分
- c++中的hash map
- poj1840 map + hash
- 缓存机制常见问题
- 遍历获取list集合的值并通过aJax传给后端
- 使用VisualSVN Server搭建SVN服务器
- java.lang.NoClassDefFoundError: javax/mail/Message
- 浅谈require和import
- hash(二分+hash)使用map会tle
- Unity3D
- Idea Maven 整合ssm框架: SpringMVC+Spring+MyBatis
- 华为面试题(8分钟写出代码) 有两个数组a,b,大小都为n,数组元素的值任意,无序; 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小
- ffmpeg的工具ffplay.exe使用
- Git小结(入门篇)
- jscpd--前端代码重复率检测
- hdu 3966 Aragorn's Story
- 用Java实现二叉链表方式存储的二叉树