hash 【模板】】的更多相关文章

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同的子串个数? 做法1:字符串hash 一般的做法就是模拟每段子串的长度L(从1到 len),这样时间复杂度为O(n^2);但是里面并没有计算子串比较是否相等以及存储的时间,而这段时间就是将字符串看成是“数字”,这样存储和比较的时间均降为O(1)了: 下面讲讲模板: 1.如何将字符串看成一个“高精度的…
线段树 #include<cstdio> using namespace std; int n,p,a,b,m,x,y,ans; struct node { int l,r,w,f; }tree[]; inline void build(int k,int ll,int rr)//建树 { tree[k].l=ll,tree[k].r=rr; if(tree[k].l==tree[k].r) { scanf("%d",&tree[k].w); return; } ;…
http://acm.hdu.edu.cn/showproblem.php?pid=4080 求出现次数大于等于n的最长串. #include<iostream> #include<cstdlib> #include<cstdio> #include<vector> #include <string> #include <map> #define LL long long using namespace std; ; ;//23333…
题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; ; ; inline LL read()//输入外挂 { ,f=; ;ch=getchar();} +ch-';ch=getchar();} return x*f; } struct hashmap//建立哈希…
;//一般为靠近总数的素数 struct Hashtable { int x;//hash存的值 Hashtable * next; Hashtable() { next = ; } }; Hashtable * Hash[mod]; void Hash_Insert(int x)//存x { int key = x % mod;//hash函数,根据情况而定 if(!Hash[key])//该key第一个元素 { Hashtable * p = new Hashtable; p->x = x;…
//注意MAXN是最大不同的HASH个数,一般HASHN是MAXN的两倍左右,MAXLEN表示字符串的最大长度 //K表示正确率,越大正确率越高,当时也越费空间,费时间. //使用前注意初始化hash_init(); //用法参考下面注释程序. //HashNode里面可以储存很多信息,注意灵活使用. 内存如果可能溢出可修改使得内存减一半,但是注意乘法溢出. #define HASHN 1000007 #define MAXN 500000 #define MAXLEN 500500 #defi…
题意:问是否存在一段区间其加减交错和为K. 显然,我们可以用set保存前缀和,然后枚举一个端点查找.具体的 若在st1中查找 $t$,为 $sum-t=-k$,在st2中则是 $sum-t=k$. 注意这样作差的话,没有考虑到 $a_1$ 开始的,只要在st2中插入一个0即可. 然而,这题卡set,必须手写hashmap(说实话,之前不太相信会有卡set的题!) 后来发现unordered_set也能过(刚好过) 分别是unordered_set.set.手写hashmap #include<b…
typedef long long ll; typedef unsigned long long ull; #define maxn 1005 struct My_Hash { ull ; ull p[maxn],ha[maxn]; void Insert(char s[]) { ); p[]=,ha[]=; ;i<=len;i++) { p[i]=p[i-]*base; ha[i]=ha[i-]*base+(ull)s[i]; } } ull gethash(int l,int r) { ]*…
一.字符串HASH模板  取自挑战程序设计竞赛(第2版) </pre><pre code_snippet_id="446698" snippet_file_name="blog_20140809_1_9461278" name="code" class="cpp">/*===================================================*\ 从b串中寻找和a串长度同样的…
维护前缀和sum[i]=a[0]-a[1]+a[2]-a[3]+…+(-1)^i*a[i]枚举结尾i,然后在hash表中查询是否存在sum[i]-K的值.如果当前i为奇数,则将sum[i]插入到hash表中.上面考虑的是从i为偶数为开头的情况.然后再考虑以奇数开头的情况,按照上述方法再做一次即可.不同的是这次要维护的前缀和是sum[i]=-(a[0]-a[1]+a[2]-a[3]+…+(-1)^i*a[i])I为偶数的时候将sum[i]插入到hash表.总复杂度o(n) 注意一个tips:sca…