HDU 4080 UVaLive 4513 - Stammering Aliens (字符串hash)
来源:互联网 发布:java context 编辑:程序博客网 时间:2024/06/13 18:20
Stammering Aliens
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 745 Accepted Submission(s): 283
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
Recommend
lcy | We have carefully selected several similar problems for you: 4072 4071 4074 4075 4076
题意:
给定一个字符串,找出至少出现m次的最长子串
先二分子串长度Len,然后判断Len是否可行:
先把所有Len长度的字串hash计算出来,然后排序后扫一遍有没有超过m个hash值相同,如果有则说明当前Len可行。
hash值试了好几个都会WA
#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define P64I1(a) printf(oform1, (a))#define REP(i, n) for(int (i)=0; (i)<n; (i)++)#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 1e-9;const double PI = (4.0*atan(1.0));const int maxn = 40000 + 20;const int x = 123;unsigned long long H[maxn], xp[maxn];unsigned long long hash[maxn];int rank[maxn];char str[maxn];int n, m;bool cmp(int a, int b) { if(hash[a] != hash[b]) return hash[a] < hash[b]; return a < b;}int judge(int L) { int pos = -1; for(int i=0; i<=n-L; i++) { rank[i] = i; hash[i] = H[i] - H[i+L] * xp[L]; } sort(rank, rank+n-L+1, cmp); int c = 0; for(int i=0; i<=n-L; i++) { if(!i || hash[rank[i]] != hash[rank[i-1]]) c = 0; if(++c >= m) pos = max(pos, rank[i]); } return pos;}int main() { while(scanf("%d", &m) != EOF && m) { scanf("%s", str); n = strlen(str); xp[0] = 1; for(int i=1; i<=n; i++) xp[i] = xp[i-1] * x; H[n] = 0; for(int i=n-1; i>=0; i--) H[i] = H[i+1] * x + str[i]-'a'; if(judge(1) < 0) { puts("none"); continue; } int L = 1, R = n; int ansL = 0, ansP = 0; while(L <= R) { int M = (L + R) >> 1; int ret = judge(M); if(ret >= 0) { if(M > ansL || (M==ansL && ret > ansP)) { ansL = M; ansP = ret; } L = M + 1; } else R = M - 1; } printf("%d %d\n", ansL, ansP); } return 0;}
0 0
- HDU 4080 UVaLive 4513 - Stammering Aliens (字符串hash)
- Uvalive 4513 Stammering Aliens(字符串Hash)
- UVALive 4513 Stammering Aliens 字符串Hash求LCS
- LA 4513 Stammering Aliens(字符串hash)
- UVALive 4513 Stammering Aliens hash+二分
- UVALive - 4513 Stammering Aliens
- LA 4513 Stammering Aliens 字符串hash
- hdu 4080 Stammering Aliens 二分 hash
- hdu4080 Stammering Aliens(二分+字符串hash)
- UVALive 4513 (LA 4513) Stammering Aliens 后缀数组 或 hash
- UVALive 4513 Stammering Aliens (hash+二分 or 后缀数组)
- uvalive 4513 Stammering Aliens (基于Hash的LCP+二分)
- la4513 Stammering Aliens 字符串hash
- LA - 4513 - Stammering Aliens(Hash + 二分)
- [HDU 4080] Stammering Aliens (字符串哈希+二分)
- HDU 4080 Stammering Aliens
- HDU 4080 Stammering Aliens(后缀数组)
- UVA 12206 Stammering Aliens(字符串hash)
- 博客已搬家
- Python3.4.1爬虫编写笔记
- iOS7 新UI 一
- 通过金矿模型介绍动态规划
- 数据结构之字符串
- HDU 4080 UVaLive 4513 - Stammering Aliens (字符串hash)
- UIWebView项目中在多个webview加入自定义全局UserAgent
- [leetcode]107.Binary Tree Level Order Traversal II
- 第八周项目二计算加班费
- iOS7 新UI 二
- 我是如何通过别人的淘宝店,在45天内成功招到600学员的
- 【树链剖分】 POJ 3237 Tree
- 什么是操作数
- 100万 素数表 (is2120)