[POJ 1200]Crazy Search

[POJ 1200]Crazy Search

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

题意:

给你一个字符串,这个字符串中有nc个不同的字母,询问在这个字符串中有多少个不同的长度为n的子串。

题解:

一道经典的哈希题。

可以根据进制来进行哈希,因为字符串中有nc个不同的字母,我们不妨就把字符串中的每一个字母转化为nc进制的一个数。然后将其对应十进制在转移到哈希数组中。

那么怎么确定哈希数组的大小呢?题目中说“You may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 Millions.” 翻译过来就是不同子串的数目不超过16000000,然后无脑将哈希数组调成16000000就可以了。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
int n,len,m,b[26],cnt,ans;
char s[1000005];
bool hash[16000000];
int main()
{
    int i,j;
    memset(b,-1,sizeof(b));
    scanf("%d%d",&n,&m);
    scanf("%s",s+1);
    len=strlen(s+1);
    for(i=1;i<=len;i++)
    if(b[s[i]-'a']==-1)b[s[i]-'a']=++cnt;
    int to=len-n+1;
    for(i=1;i<=to;i++)
    {
        int key=0;
        for(j=i;j<=i+n-1;j++)
        {
            key=key*m+b[s[j]-'a'];
        }
        if(!hash[key])
        {
            ans++;
            hash[key]=1;
        }
    }
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/huangdalaofighting/p/7360098.html