# [POJ 1200] Crazy Search【字符串Hash】

• 2018-01-07
• 0
• 0

## Problem:

 Time Limit: 1000MS Memory Limit: 65536K

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.

Sample Input

```3 4
daababac```

Sample Output

`5`

Hint

Huge input,scanf is recommended.

Source

Southwestern Europe 2002

## Code: O(L), 其中L为字符串长度 [7884K, 32MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int N, NC, ans;
char str[16000005];
int id[128], topid = 0;
bool exi[16000005];
// Ambiguous data size is given so that the size of exi[] is not determined

inline int fastpow(int bas, int ex){
int res = 1;
while(ex){
if(ex & 1) res *= bas;
ex >>= 1, bas *= bas;
}
return res;
}

int main(){
scanf("%d%d%s", &N, &NC, str);
int len = strlen(str), topHash = fastpow(NC, N - 1);
// Get length of str and weight of top digit of a N-digited number
for(register int i = 0; i < len; i++)
if(!id[str[i]]) id[str[i]] = topid++;  // Distribution the NC identities
int curHash = 0;
for(register int i = 0; i < N; i++) curHash = curHash * NC + id[str[i]];
exi[curHash] = 1, ans = 1;
for(register int i = N; i < len; i++){
curHash = (curHash - topHash * id[str[i - N]]) * NC + id[str[i]];
if(!exi[curHash]) exi[curHash] = 1, ans++;
}
printf("%d\n", ans);
return 0;
}
```

#### 评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai