The weather is good this day, hzhwcmhf god Ben to vfleaking out a question:
Give you a string s of length n, and ask how many substrings of a different length are L.
The definition of a substring is s[l], S[l + 1] 、... S[R] Such a continuous paragraph.
Two strings are considered different when and only if the characters in a location are different.
Vfleaking see that this is not a hash of the naked problem! So decisively wrote the hash + sort.
and HZHWCMHF god Ben Heart naturally know, this problem is the suffix array height in the number of < L + 1, is the suffix of the machine on the length of the number of nodes containing L, is the suffix tree depth is the number of nodes.
But HZHWCMHF god Ben looked at Vfleaking's practice to show very sweat. So he wanted to get him off the cards.
Vfleaking uses a dictionary-ordered hash whose code is roughly the following:
U64 val = 0;
for (int i = 0; i < l; i++)
val = (val * base + s[i]-' a ')% Mod;
U64 is unsigned int64, the range is [0, 2^64).
base is a constant, and vfleaking determines its value based on mood.
mod equals 1000000007.
Vfleaking also found the base ^ l% mod, which is the L sub-division of the base divided by the remainder of the MoD, which makes it easy to find hashes of all substrings of length L.
The vfleaking then sorts the hashes, goes to the weight, and evaluates how many different hashes to use as the result.
The C + + code for its algorithm is as follows:
typedef unsigned long long u64;
const int MAXN = 100000;
inline int hash_handle (const char *s, const int &n, const int &L, const int &base)
{
const int Mod = 1000000007;
U64 hash_pow_l = 1;
for (int i = 1; I <= l; i++)
hash_pow_l = (hash_pow_l * base)% Mod;
int li_n = 0;
static int LI[MAXN];
U64 val = 0;
for (int i = 0; i < l; i++)
val = (val * base + s[i]-' a ')% Mod;
li[li_n++] = val;
for (int i = l; i < n; i++)
{
val = (val * base + s[i]-' a ')% Mod;
val = (val + mod-((S[i-l]-' a ') * hash_pow_l)% mod)% MoD;
li[li_n++] = val;
}
Sort (Li, Li + li_n);
Li_n = Unique (Li, Li + li_n)-li;
return li_n;
}
HZHWCMHF of course know how to card! But he wants to test you.
Industry conscience Kahashi!!!
People like me who never thought of using a hash really can't bear the pain of being Hashi the past.
Birthday paradox: If there are 23 or 23 people in a room, then at least two people have the same probability of birthdays being greater than 50%.
Birthday attack: A random number in N, approximately sqrt (n) can fetch two identical numbers
That is, for a [0,n-1] hash table, the deposit sqrt (n) times will probably collide once
Then you can use this method card, build a length of sqrt (modulus) of the string, the topic directly = 10^5
And then for the value of L, just make sure L is the hash value of the string over modulus.
Or it won't happen.
[BZOJ3098] Hash Killer II Problem solving report