Crazy Search
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26713 Accepted: 7449

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.

(养成翻译的好习惯)给定字符串,其中字符集大小不超过nc,求其中长度为n的不同的子串个数

第一不要dp做多了把子串看成不连续的

子串就是源字符串连续的子序列!这点看题解才发现……想了半天也想不出来

接下来就好办多了,枚举每一位即可

但问题又来了,如何去重?kmp不行,ac自动机没试过不会,但目测仍然超时

接下来由rk-hash实力打脸kmp!

o(len)的速度没的说,而且已知hash值的话只用o(1)就能办到

rk-hash是什么?把字符串看成一个整数的高精度即可(请自行百度)

但算出哈希还不够,hash值应该会很大,所以要再用一次哈希,模一个素数,模拟链表处理冲突

这样大概空间时间就差不多了

但!but!

“字符集大小”并不意味着按照abcde的顺序给出!

所以单个字符对应的hash值还得自己做出来(具体看代码)

 1 //子串还必须是连续的(不然无解了)
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 int base,len;
 6 const int mod=;
 7 char read[];
 8 int ex[]={};//单个字符值
 9 int hash[mod+][]={{}};
 int get(int pos){
     int ans=;
     for(int i=pos;i<pos+len;i++){
         ans*=base;
         ans+=ex[read[i]];
     }
     int tmp=ans%mod;
     if(hash[tmp][])for(int i=;i<=hash[tmp][];i++)if(hash[tmp][i]==ans)return ;
     hash[tmp][]++;
      hash[tmp][hash[tmp][]]=ans;
      return ;
 }
 int main(){
     scanf("%d %d\n%s",&len,&base,read);
     int le=strlen(read);
     for(int i=,j=;i<le;i++){
         if(!ex[read[i]])ex[read[i]]=++j;
         if(j==base)break;//很简洁地处理字符对应关系
     }
     int ans=;
     for(int i=;i<=le-len;i++)ans+=get(i);
     printf("%d\n",ans);
     return ;

33 }

[poj1200]Crazy Search(hash)的更多相关文章

  1. POJ-1200 Crazy Search,人生第一道hash题!

                                                        Crazy Search 真是不容易啊,人生第一道hash题竟然是搜博客看题解来的. 题意:给你 ...

  2. POJ1200 Crazy Search

    Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description Many peo ...

  3. poj 1200 Crazy Search(hash)

    题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...

  4. hdu1381 Crazy Search(hash map)

    题目意思: 给出一个字符串和字串的长度,求出该字符串的全部给定长度的字串的个数(不同样). 题目分析: 此题为简单的字符串哈hash map问题,能够直接调用STL里的map类. map<str ...

  5. POJ1200 A - Crazy Search(哈希)

    A - Crazy Search Many people like to solve hard puzzles some of which may lead them to madness. One ...

  6. hdu 1381 Crazy Search

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1381 Crazy Search Description Many people like to sol ...

  7. (map string)Crazy Search hdu1381

    Crazy Search Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. POJ 1200:Crazy Search(哈希)

    Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32483   Accepted: 8947 Des ...

  9. Crazy Search POJ - 1200 (字符串哈希hash)

    Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...

随机推荐

  1. zepto源码--核心方法6(显示隐藏)--学习笔记

    在不引入zepto插件模块fx_metho其他ds的情况下,zepto默认的显示隐藏的函数只有show, hide, toggle,这里解释有个前提条件,就是没有引入zepto的fx_methods插 ...

  2. 单身狗进化——求n!的位数

    题目: 分析: 这道题目要求的是n!的位数,显然一种思路是先求出n!的值,假定为res,然后再计算res的位数,这种方法在n比较小时是可以的,如果res为int型,一旦n>16,res就会超出i ...

  3. MacOS10.11的/usr/bin目录不可写后class-dump的处理办法

    许多升级了OSX 10.11的朋友在配置class-dump的时候,会发现书上推荐的class-dump存放目录/usr/bin不再可写,如下所示: 192:~ snakeninny$ touch c ...

  4. open falcon dashboard 安装

    open falcon dashboard 安装 yum -y install lrzsz python-virtualenv mysql-devel python-devel libffi-deve ...

  5. django使用gmail

    POSTED ON 02 JUL 2007 IN DEVELOPMENT DJANGO PYTHON WEBDid a bit of running around today to get Djang ...

  6. SQL-乐观锁,悲观锁之于并发

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  7. Centos7中源码安装Nodejs

    Step 1.确认服务器有nodejs编译及依赖相关软件,如果没有可通过运行以下命令安装. [root@BobServerStation local]# yum -y install gcc gcc- ...

  8. asp获取文件名和扩展名的函数代码

    <% '获取文件名(不含扩展名) Function getFilename(text)text = Left(text,inStrRev(text,".")-1)getFil ...

  9. 转:SVN服务器搭建和使用(三)

    http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html 接下来,试试用TortoiseSVN修改文件,添加文件,删除文件, ...

  10. Unity随机随学

    1.什么是渲染管道? 是指在显示器上为了显示出图像而经过的一系列必要操作.渲染管道中的步骤很多,都要将几何物体从一个坐标系中变换到另一个坐标系中去. 主要步骤有: 本地坐标->视图坐标-> ...