poj 1200 Crazy Search(hash)】的更多相关文章

题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, 可以再线性时间内解决问题:问题关键在与Hash函数的选择,使得子串之间的Hash值不同:由于NC的提示,使用NC作为基数,其他字符 分配不同的数码,从1-NC,再求取Hash值,保证函数为单一映射: 代码如下: #include <stdio.h> #include <string.h>…
题目:http://poj.org/problem?id=1200 最近看了一个关于hash的问题,不是很明白,于是乎就找了些关于这方面的题目,这道题是一道简单的hash 字符串题目,就先从他入手吧. 题目说字串的最大数量不超过16Millions,也就是字串的存储16000000就够了. 查看网上给出的hash映射是把字串映射成为一个NC进制的数字每个字串都是一个数字. #include <stdio.h> #include <iostream> using namespace…
<题目链接> 题目大意: 给定n,nc,和一个字符串,该字符串由nc种字符组成,现在要你寻找该字符串中长度为n的子字符串有多少种. 解题分析: 因为要判重,所以讲这些字符串hash一下,将不同的字符串映射为数字,这里我们是将该字符串转化为nc进制数,不同的字符串分别对应nc进制下不同的数. #include <cstdio> #include <cstring> using namespace std; ; char str[M]; bool hash[M]; ]; i…
RK法:https://www.cnblogs.com/16crow/p/6879988.html #include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include<iostream> #include<cstring> #include<set> #include<queue> #include<algorithm&…
第一次涉及HASH查找的知识 对于字符串的查找有很多前人开发出来的HASH函数,比较常用的好像是ELF 和 BKDR. 这道题没想到突破点是在于其nc值,告诉你组成字符串的字母种类. 还有用26进制,不管怎么说,只要避免产生冲突,怎么哈希都行. 用的是BKDRHash法. #include <iostream> #include <cstdio> #include <cstring> #define maxn 20000000 #define mm 1000000 us…
题目链接 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…
http://poj.org/problem?id=1200 #include<iostream> #include<cstring> using namespace std; const int maxn=2e7; ],ans=; ]; char s[maxn]; int main(){ memset(vis,,); scanf("%d%d%s",&len,&base,s); int base1=base; //把字符串转换为base进制的数…
思路:利用Karp-Rabin算法的思想,对每个子串进行Hash,如果Hash值相等则认为这两个子串是相同的(事实上还需要做进一步检查),Karp-Rabin算法的Hash函数有多种形式,但思想都是把字符串映射成一个数字.本题hash函数是把字串转化为NC进制的数(实际上程序中计算结果已经被转换为10进制,因为NC进制数不同转化为10进制数自然不同,所以不影响判断结果),数组开到了1.6×10^7(我试了一下1.2×10^7也能AC),实际上这也是不严谨的,因为我们不能保证hash之后的数值在这…
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…
题目意思: 给出一个字符串和字串的长度,求出该字符串的全部给定长度的字串的个数(不同样). 题目分析: 此题为简单的字符串哈hash map问题,能够直接调用STL里的map类. map<string,int> snum; AC代码: #include<iostream> #include<string> #include<map> using namespace std; int main() { int t,n,nc; cin>>t; whi…