# ZOJ Crazy Search【哈希函数】

Crazy Search

Time Limit: 5 Seconds      Memory Limit: 32768 KB

Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a given text. Such number could be the number of different substrings of a given size that exist in the text. As you soon will discover, you really need the help of a computer and a good algorithm to solve such a puzzle.

Your task is to write a program that given the size, N, of the substring, the number of different characters that may occur in the text, NC, and the text itself, determines the number of different substrings of size N that appear in the text.

As an example, consider N=3, NC=4 and the text "daababac". The different substrings of size 3 that can be found in this text are: "daa", "aab", "aba", "bab", "bac". Therefore, the answer should be 5.

Input

The first line of input consists of two numbers, N and NC, separated by exactly one space. This is followed by the text where the search takes place. You may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 Millions.

Output

The program should output just an integer corresponding to the number of different substrings of size N found in the given text.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input

1

3 4
daababac

Sample Output

5

Source: Southwestern Europe 2002

对于一个字符串，问其长度为n的字串有多少种？字符的种类不会超过nc种。

将出现的字母映射为自然数0到n-1，n为出现的字母的数量。这样做的好处是算出来的哈希值（整数）比较小。

使用nc作为计算哈希值时seed。

这个题，需要注意存储限制，只有32768K，容易存储溢出，需要使用布尔数组来解决。时间上似乎不是问题，给的是5s。需要特殊的处理。

AC的C++语言程序如下：
/* ZOJ Crazy Search */

#include<iostream>
#include<string>
#include <string.h>

using namespace std;

const int N = 16e6;
bool hashval[N + 1];
char digit[26];

int main()
{
int t, n, nc;
cin >> t;
while(t--) {
memset(hashval, 0, sizeof(hashval));
memset(digit, 0, sizeof(digit));

string s;
cin >> n >> nc >> s;

int k = 0;
for(int i = 0; s[i]; i++)
if(digit[s[i] - 'a'] == 0)
digit[s[i] - 'a'] = k++;

int len = s.size() - n + 1;
int ans = len;
for(int i=0; i < len; i++) {
int hv = 0;
for(int j = i; j < i + n; j++)
hv = hv * nc + digit[s[j] - 'a'];
hv %= N;

if(hashval[hv])
ans--;
else
hashval[hv] = true;
}

cout << ans << endl;

if(t)
cout << endl;
}

return 0;
}


• 评论

• 上一篇
• 下一篇