Crazy Search

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3149    Accepted Submission(s): 1174

Problem Description
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.
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
13 4daababac

Sample Output
5

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

这个题的一种解法是，取出所有长度为n的字串放入集合中，数一下就好了。

另外一种解法是，用哈希函数计算各个字串的哈希值，放入集合中数一下就好了。但是，这里给出的程序没有直接使用计算哈希值的函数来实现，稍微用了一点技巧，计算时间要稍微快一些。

好在，这个题测试数据不大，不需要用更多的技巧。

AC的C++语言程序（哈希函数）如下：
/* HDU1381 UVA1123 UVALive2680 Crazy Search */

#include <iostream>
#include <string>
#include <set>

using namespace std;

#define getHashval(n, l) hv[n] - hv[n+l] * nbase[l]

typedef unsigned long long ULL;

const int SEED = 131;
const int N = 1e6;
ULL nbase[N + 1];
ULL hv[N + 1];

int main()
{
nbase[0] = 1;
for(int i = 1; i <= N; i++)
nbase[i] = nbase[i-1] * SEED;

int t, n, nc;
cin >> t;
while(t--) {
set<ULL> ss;
string s;
cin >> n >> nc >> s;

// 计算长度为l，各个字串的哈希值
int len = (int)s.length();
hv[len] = 0;
for(int i = len - 1; i >= 0; i--)
hv[i] = hv[i + 1] * SEED + s.at(i);

for(int i = 0; i <= len - n; i++)
ss.insert(getHashval(i, n));

cout << ss.size() << endl;

if(t)
cout << endl;
}

return 0;
}



AC的C++语言程序（字串集合）如下：

/* HDU1381 UVA1123 UVALive2680 Crazy Search */

#include <iostream>
#include <string>
#include <set>

using namespace std;

int main()
{
int t, n, nc;
cin >> t;
while(t--) {
set<string> ss;
string s;
cin >> n >> nc >> s;

int len = s.length();
for(int i = 0; i <= len - n; i++) {
string subs = s.substr(i, n);
ss.insert(subs);
}

cout << ss.size() << endl;

if(t)
cout << endl;
}

return 0;
}



删除_纪中1381_暴力

2016-07-13 20:09:30

VIJOS 1212 Way Selection

2015-09-05 21:51:41

Vijos P1696 数与连分数【连分数】

2017-05-24 08:59:10

Vijos P1398 奖学金【排序】

2017-07-15 06:29:30

Vijos P1098 合唱队形（动态规划，双重LIS）

2016-08-25 08:18:06

Vijos P1335 数独验证【谜题】

2017-05-23 22:57:27

Vijos P1313 金明的预算方案（动态规划，有依赖的背包）

2016-08-22 17:24:07

智力题——桌子上轮流放置硬币怎样才能赢得游戏的胜利

2017-07-20 20:43:21

hdu1381 Crazy Search(hash map)

2014-11-10 22:41:01

hdu 1381 Crazy Search

2015-07-06 10:34:50