[poj1200]Crazy Search(hash)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26713 | Accepted: 7449 |
Description
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
Output
Sample Input
- 3 4 daababac
Sample Output
- 5
Hint
(养成翻译的好习惯)给定字符串,其中字符集大小不超过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)的更多相关文章
- POJ-1200 Crazy Search,人生第一道hash题!
Crazy Search 真是不容易啊,人生第一道hash题竟然是搜博客看题解来的. 题意:给你 ...
- POJ1200 Crazy Search
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description Many peo ...
- poj 1200 Crazy Search(hash)
题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...
- hdu1381 Crazy Search(hash map)
题目意思: 给出一个字符串和字串的长度,求出该字符串的全部给定长度的字串的个数(不同样). 题目分析: 此题为简单的字符串哈hash map问题,能够直接调用STL里的map类. map<str ...
- POJ1200 A - Crazy Search(哈希)
A - Crazy Search Many people like to solve hard puzzles some of which may lead them to madness. One ...
- hdu 1381 Crazy Search
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1381 Crazy Search Description Many people like to sol ...
- (map string)Crazy Search hdu1381
Crazy Search Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- POJ 1200:Crazy Search(哈希)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32483 Accepted: 8947 Des ...
- Crazy Search POJ - 1200 (字符串哈希hash)
Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...
随机推荐
- app启动调用的api
(8)在app启动时,调用一个初始化api获取必要的信息 通过这个初始化api,获取一下必要的信息,例如,最新的app版本.当发现本地app的版本已经低于最新的app版本,可提示用户更新.当然了,这个 ...
- 定时调度模块:sched
"""A generally useful event scheduler class. 事件调度器类 Each instance of this class manag ...
- bash 常用操作
删除不为空的文件夹 rm -rf dir_name
- MVC4脚本压缩 BundleTable bundles 404错误
在发布网站的时候,因为使用了MVC4的新特性BundleTable,造成访问的时候js和css报了404错误, google了以后, 有朋友说是因为要在webservice添加 <modules ...
- 一些IT中的工具介绍【转】
1. 史上最全github使用方法:github入门到精通 2. Git教程 3. GIT与GitHub使用简介 简单来说,git是一种版本控制系统.跟svn.cvs是同级的概念.github是一 ...
- SqlServer判断表是否存在
.判断数据表是否存在 方法一: use yourdb; go if object_id(N'tablename',N'U') is not null print '存在' else print '不存 ...
- .NET 框架基本原理透析⑴
.NET框架的核心便是通用语言运行时(CLR),顾名思义它是一个可被各种不同的编程语言所使用的运行时.CLR的很多特性可用于所有面向它的编程语言.比如,如果CLR用异常来报告错误,那么所有面向它的语言 ...
- Diablo2 1.13版&PlugY10.00 男巫存档
下载地址: http://files.cnblogs.com/files/xiandedanteng/20160805D2113NanwuL83Backup.rar 解压后文件放到Diablo2游戏的 ...
- 刨根问底U3D---如何退出Play模式后保留数据更改
实际中遇到的需求 在做一款对抗类游戏,目前正在调整游戏的平衡性 所以就产生了一个需求 希望可以在Play模式时候对数据源做的更改可以在退出时候被保存下来. 举个Case, 比如 有一个炮塔 可以发射子 ...
- 在Unity中如何取得一个Box的Bounds
private BoxCollider mCollider; // Use this for initialization void Start () { mCollider = GetCompone ...