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. php读取文件里面的数组做为配置文件

    可能大家也都见过很多开源的产品,大多它们的配置文件都存放在一个单独的文件中,而这个文件里只存放了一个数组,其实这里运用了一个PHP的小技巧,就是可以将文件包含进来,并且赋值给一个变量,这个变量就具有了 ...

  2. token原理

    token原理1.和session有很大关系哦. jsp生成表单时,在表单中插入一个隐藏<input>字段,该字段就是保存在页面端的token字符串,同时把该字符串存入session中.等 ...

  3. Java学习-043-获取文件在目录中的路径

    我们在日常的电脑使用中,经常需要在当前目录或当期目录及其子目录中查找文件,并获取相应的文件路径名.在我们的自动化测试中,也经常需要确认文件在目录中是否成功生成或已存在,因而我写了一个小方法来实现. 获 ...

  4. JAVASE02-Unit02: 正则表达式 、 Object 、 包装类

    正则表达式 . Object . 包装类 字符串支持正则表达式的方法一: package day02; /** * 字符串支持正则表达式的方法一: * boolean matches(String r ...

  5. [原创]java WEB学习笔记93:Hibernate学习之路---Hibernate 缓存介绍,缓存级别,使用二级缓存的情况,二级缓存的架构集合缓存,二级缓存的并发策略,实现步骤,集合缓存,查询缓存,时间戳缓存

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. Android之下载管理者

    public interface HttpDownloader { public void setDownloadManager(HttpDownloadManager manager); publi ...

  7. SpringMvc自定义拦截器

    SpringMvc也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义拦截器必须实现HandlerInterceptor接口 -preHandle():这个方法在业务处理器 ...

  8. windows+caffe(三)——求取图片的均值

    这个要在图片已经转化成lmdb格式下才能求均值... 1.查看caffe根目录下的bin是否存在compute_image_mean.exe(用的happey大神的) 如果没有存在,你需要打开Main ...

  9. Asp.Net在多线程环境下的状态存储问题

    在应用开发中,我们经常需要设置一些上下文(Context)信息,这些上下文信息一般基于当前的会话(Session),比如当前登录用户的个人信息:或者基于当前方法调用栈,比如在同一个调用中涉及的多个层次 ...

  10. 如何生成DLL文件

    1.打开项目工程,点击Rebuild 2.Rebuild成功后,打开该项目所在文件目录 3.在路径里,在bin->Debug文件下可以看到刚生成成功的dll文件.