LA 4513 (Stammering Aliens-Hash for LCP) [Template: hash for LCP], 4513lcp
4513-Stammering AliensDr. ellie Arroway has established contact with an extraterrestrial civilization. however, all efforts to decode their messages have failed so far because, as luck wowould 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
BabTwice, they might just send the message
Babab, Which has been abbreviated because the second
BOf the first word can be reused as the first
BOf 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 integerM, And a stringS, Representing the message, your task is to find the longest substringSThat appears at leastMTimes. For example, in the messageBaaaababababbababbab, The length-5 wordBababIs contained3 times, namely at positions5, 7 and 12 (where indices start at zero ). no substring appearing3 or more times is longer (see the first example from the sample input ). on the other hand, no substring appears11 times or more (see example 2 ).
In case there are several solutions, the substring with the rightmost occurrence is preferred (see example3 ).
Input The input contains several test cases. Each test case consists of a line with an integer
M(
M1), the minimum number of repetitions, followed by a line containing a string
SOf length
MAnd 40 000, random Sive. All characters in
SAre lowercase characters from ''a' to ''z''. The last test case is denoted
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
MTimes; the second integer gives the rightmost possible starting position of such a substring.
Sample Input
3baaaababababbababbab11baaaababababbababbab3cccccc0
Sample Output
5 12none4 2
This topic is an introduction to how to calculate LCP by Hash in the lrj White Book.
Refer to the White Book for more information.
# Include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> # include <functional> # include <iostream> # include <cmath> # include <cctype> # include <ctime> using namespace std; # define For (I, n) for (int I = 1; I <= n; I ++) # define Fork (I, k, n) for (int I = k; I <= n; I ++) # define Rep (I, n) for (int I = 0; I <n; I ++) # define ForD (I, n) for (int I = n; I --) # define RepD (I, n) for (int I = n; I> = 0; I --) # define Forp (x) for (int p = pre [x]; p = Next [p]) # define Forpiter (x) for (int & p = iter [x]; p = next [p]) # define Lson (x <1) # define Rson (x <1) + 1) # define MEM (a) memset (a, 0, sizeof (a); # define MEMI () memset (a, 127, sizeof (a); # define MEMi (a) memset (a, 128, sizeof (a); # define INF (2139062143) # define F (100000007) # define MAXN (40000 + 10) typedef long ll; typedef unsigned long ull; ll mul (ll a, ll B) {return (a * B) % F;} ll add (ll a, ll B) {return (+ B) % F;} ll sub (ll a, ll B) {return (a-B + (a-B)/F * F + F) % F ;} void upd (ll & a, ll B) {a = (a % F + B % F) % F ;}int n, m; char s [MAXN]; const int x = 143; ull H [MAXN], xp [MAXN], hash [MAXN]; int rank [MAXN]; int cmp (const int &, const int & B) {return hash [a]