poj1200Crazy Search

来源:互联网 发布:深圳ug数控编程培训 编辑:程序博客网 时间:2024/05/22 03:10


http://poj.org/problem?id=1200

Language:
Crazy Search
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 25786 Accepted: 7216

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 4daababac

Sample Output

5

Hint

Huge input,scanf is recommended.

Source

Southwestern Europe 2002

题目的大致意思是给定两个数n,nc,n代表多少个字符一组,nc代表做多出现多少种字符;

第二行数输出一个字符串,

输出的结果是输出这个一个字符串中n个一组,有多少种可能


题目分析:

典型hash,把每个字符看成nc进制的数进行运算,(也可=可以根据情况把c进制转换为十进制)

需要注意的是题目中给出最长的子链的数值不能超过一千六百万,这就代表转换为nc进制的数最大可能有一千六百万个,所以对nc进制取模的时候,必须要取大于一千六百万的数,如果取得是小于一千六百万吨额数就可能造成键值冲突,当然按照这道题来说也是可以不用去摸操作的(因为数过大)。

#include<cstdio>#include<cstring>#include<algorithm>#include<set>#include<map>#include<string>using namespace std;set<int> s;map<char,int> m;#define MAXN 1000000int q=16000000;  //一个大素数bool vis[8000000]; //定义一个标记数组void search(char* str,int n,int d){    int h=1;   //计算出nc进制的最高位    int sum=0;  //一共有多少个不一样的数    long long hash=0;  //定义hash值    int len = strlen(str);    int cnt=1;   //最高是nc-1,先从1开始的原因是map在主函数都清空了,第一个数不能是0,如果第一个数是0的话可能会和后面的数有冲突    for(int i=0;i<len;i++)        if(!m[str[i]])            m[str[i]] = cnt++;    for(int i=0;i<n-1;i++)        h=(h*d)%q;   //得到nc禁止的最高位    for(int i=0;i<n;i++)        hash = (d*hash + m[str[i]]-1)%q;    //计算前n个的hash值    for(int i=0;i<=len-n;i++)   //进行比较    {        if (!vis[hash])        {            sum++;            vis[hash] = true;        }        hash = (d*(hash-(m[str[i]]-1)*h)+m[str[i+n]]-1)%q;//下n个的hash值        //if(hash<0)        //    hash+=q;   //如果hash小于零就取反    }    printf("%d\n",sum);}int main(){    int n,d;    char str[MAXN];    while(~scanf("%d%d",&n,&d))    {        memset(vis, false, sizeof(vis));        m.clear();        scanf("%s",str);        search(str,n,d);    }    return 0;}



1 0
原创粉丝点击