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
3 baaaababababbababbab 11 baaaababababbababbab 3 cccccc 0
Sample Output
5 12 none 4 2
Source
Recommend
题意:
给定一个字符串,找出至少出现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 WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef 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;
}