C++-POJ1200-Crazy Search[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…
                                                    Crazy Search 真是不容易啊,人生第一道hash题竟然是搜博客看题解来的. 题意:给你一个包含m种字符的字符串,求长度为n的不同子串有多少个. 将每个字串化为一个具体的数然后存入数组标记即可,如果重复出现肯定不用再加了.那么怎么化为一个数呢,这里用的方法是先将每个字符对应一个数,然后每个长度为n的子串就有了一个连续的数,将这个数段转化为m进制下的数即可,时间复杂度是O(len).注意…
Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u 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…
题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, 可以再线性时间内解决问题:问题关键在与Hash函数的选择,使得子串之间的Hash值不同:由于NC的提示,使用NC作为基数,其他字符 分配不同的数码,从1-NC,再求取Hash值,保证函数为单一映射: 代码如下: #include <stdio.h> #include <string.h>…
题目意思: 给出一个字符串和字串的长度,求出该字符串的全部给定长度的字串的个数(不同样). 题目分析: 此题为简单的字符串哈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…
A - Crazy Search 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://acm.hdu.edu.cn/showproblem.php?pid=1381 Crazy Search 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 t…
Crazy Search Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3035    Accepted Submission(s): 1133     Problem Description Many people like to solve hard puzzles some of which may lead them to…
Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32483   Accepted: 8947 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 gi…
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…
<题目链接> 题目大意: 给定n,nc,和一个字符串,该字符串由nc种字符组成,现在要你寻找该字符串中长度为n的子字符串有多少种. 解题分析: 因为要判重,所以讲这些字符串hash一下,将不同的字符串映射为数字,这里我们是将该字符串转化为nc进制数,不同的字符串分别对应nc进制下不同的数. #include <cstdio> #include <cstring> using namespace std; ; char str[M]; bool hash[M]; ]; i…
题目:http://poj.org/problem?id=1200 最近看了一个关于hash的问题,不是很明白,于是乎就找了些关于这方面的题目,这道题是一道简单的hash 字符串题目,就先从他入手吧. 题目说字串的最大数量不超过16Millions,也就是字串的存储16000000就够了. 查看网上给出的hash映射是把字串映射成为一个NC进制的数字每个字串都是一个数字. #include <stdio.h> #include <iostream> using namespace…
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…
poj1200:http://poj.org/problem?id=1200 题意:给你一个有m种字符串,求长度为n的连续子串由多少种. 题解:网上的代码都是hash,但是本人觉得hash有问题,就是n,m稍微大点,hash的值都会爆出int,无法开数组来记录该串是否被记录.可能数据弱,结果hash竟然A了 .正确的解法是用set来存,把所有的hash值放进set,set有去重效果,最后求一下set的大小.但是这样结果是T了.不知道这题怎么解.一下是第一种代码.于是换别的,trie树来搞. #i…
题目链接 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…
Hash:一般是一个整数.就是说通过某种算法,可以把一个字符串"压缩" 成一个整数.一,题意: 给出两个数n,nc,并给出一个由nc种字符组成的字符串.求这个字符串中长度为n的不同子串有多少种?二,思路: 1.这个题不用匹配,因为不高效. 2.将长度为n的子串看作n位的nc进制数,将问题转化为共有多少种十进制数字. 3.哈希时,每一个字符都对应这0 ~ nc-1的一个数字.三,步骤: 1.给nc个字母编号:0 ~ nc-1 hashArray[ch[i]] = k++; 2.明确每n个…
思路:利用Karp-Rabin算法的思想,对每个子串进行Hash,如果Hash值相等则认为这两个子串是相同的(事实上还需要做进一步检查),Karp-Rabin算法的Hash函数有多种形式,但思想都是把字符串映射成一个数字.本题hash函数是把字串转化为NC进制的数(实际上程序中计算结果已经被转换为10进制,因为NC进制数不同转化为10进制数自然不同,所以不影响判断结果),数组开到了1.6×10^7(我试了一下1.2×10^7也能AC),实际上这也是不严谨的,因为我们不能保证hash之后的数值在这…
由于已经给出字符只有NC种,故可以把子串视为一个NC进制的数,以此构造hash函数就可以了 #include <set> #include <map> #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream&g…
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进制的数…
前置芝士 :string 的 基本用法 string s = "hello world" ; string tmp(s,0,5) ; cout << tmp << endl ; 上面这一段代码 可以复制粘贴 然后改变 数字.(试试效果 \(string\ tmp(s,i,n);\) 赋初值 也就是说从 $s[i] - To -  s[i+n-1] $ 所以既然是这样 应该就有一种比较优秀的做法 \(O(S.length())\) 可海星. \[STL大法好啊\]…
Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27536   Accepted: 7692 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 gi…
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33142 Accepted: 9079 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…
/* poj 1200 Crazy Search 字符串hash O(n)枚举起点 然后O(1)查询子串hash值 然后O(n)找不一样的个数 复杂度是线性的 */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define P 29 #define maxn 1000010 using namespace std; int n,c,len,p[maxn]…
#include<iostream> using namespace std; ; int num[maxn]; string s; int main() { int nc;//字符串s中不同字符的个数 cin>>s>>nc; int len=s.length(); ; num[s[]]=; ;i<len;i++) { ) { num[s[i]]=cnt++; } if(cnt==nc) { break; } } ;i<len;i++) { cout<…
一. 概念 1.引例 有线性表(1,75,324,43,1353,90,46,-  ) 目的:查找值为90的元素 常见做法: 1.通过一维数组进行遍历查找 (依次比较)( O(n) ) 2.如果关键字有序,可采用二分查找 ( O(logn) ) 缺陷:当数据规模极大的时候,查找将会变得效率低下. 假设:如果知道待查询关键字的地址,则只需要一次就可以查到. 问题:如何立刻知道关键字的地址? Hash函数: 根据关键字直接计算出元素所在位置的函数. 例:设哈希函数为:H(K)=K/3+1,则构造关键…
前言:不知道你们对url地址中的#一开始是怎么理解的,反正我以前一直都是默认那就是本页面中该id的位置.今天看了篇文章,才把这个真正透彻理解. 1,#涵义 #代表网页中的一个位置.其右面的字符,就是该位置的标识符. http://example.com/index.html#user; 就代表网页index.html的print位置.浏览器读取这个URL后,会自动将print位置滚动至可视区域. 为网页位置指定标识符,有两个方法.一是使用锚点,比如<a name="user"&g…
Hash function From Wikipedia, the free encyclopedia   A hash function that maps names to integers from 0 to 15. There is a collision between keys "John Smith" and "Sandra Dee". A hash function is any function that maps data of arbitrar…
转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Description Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden pri…
hash定义 hash这个玩意是地址栏上#及后面部分,代表网页中的一个位置,#后面部分为位置标识符.页面打开后,会自动滚动到指定位置处. 位置标识符 ,一是使用锚点,比如<a name="demo"></a>,二是使用id属性,比如 <span id="demo" ></span> 带hash的请求 当打开http://www.example.com/#print服务器实际收到的请求地址是http://www.exam…