hash讲解

  主要记录hash的公式:

for (int i=; i<=len; i++) {
Hash[i]=(Hash[i-]*base%mod+(s[i]-'A'+)%mod)%mod;
}

  求hash的公式是这个,怎么求一小段的hash值呢?

for (int i=; i<=len; i++) {
p[i]=p[i-]*base%mod;
}

ll get(int l, int r) {
  return (Hash[r]%mod-Hash[l-1]*p[r-l+1]%mod)%mod;
}

  我一直不理解为什么要乘p[r-l+1]呢?现在明白啦*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。

  首先给你一个字符串abcdef,hash[6]=a*p^5+b*p^4+c*p^3+d*p^2+e*p^1+f*p^0。如果我们要求def的hash值怎么求呢?

  我们知道hash[6]=a*p^5+b*p^4+c*p^3+d*p^2+e*p^1+f*p^0,hash[3]=a*p^2+b*p^1+c*p^0。

  而def的hash值是d*p^2+e*p^1+f*p^0,那么就看得出def的hash值是(Hash[r]%mod-Hash[l-1]*p[r-l+1]%mod)%mod这个公式。

  板子:

/*  gyt
Live up to every day */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 4e4+;
const ll maxm = 1e7;
const int mod = 1e9+;
const int INF = <<;
const db eps = 1e-;
const ll base = 1e9+;
ll Hash[maxn];
ll p[maxn];
char s1[maxn], s2[maxn]; void init(char *s) {
p[]=;
Hash[]=;
int len=strlen(s+);
for (int i=; i<=len; i++) {
p[i]=p[i-]*base%mod;
}
for (int i=; i<=len; i++) {
Hash[i]=(Hash[i-]*base%mod+(s[i]-'A'+)%mod)%mod;
}
}
ll get(int l, int r) {
return (Hash[r]%mod-Hash[l-]*p[r-l+]%mod)%mod;
}
void solve() {
while(scanf("%s", s2+)!=EOF) {
if (s2[]=='#') break;
scanf("%s", s1);
int len1=strlen(s1);
int len2=strlen(s2+);
int num=, biao=;
init(s2);
ll haha=;
for (int i=; i<len1; i++) {
haha = (haha*base%mod+(s1[i]-'A'+)%mod)%mod;
}
int ans=;
for (int i=; i+len1-<=len2; i+=len1) {
ll ha=get(i, i+len1-);
if (ha==haha) ans++;
}
printf("%d\n", ans);
}
}
int main() {
int t = ;
// freopen("in.txt", "r", stdin);
//scanf("%d", &t);
while(t--)
solve();
return ;
}

  字典树板子

  

/*  gyt
Live up to every day */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 4e4+;
const ll maxm = 1e7;
const int modd = ;
const int INF = <<;
const db eps = 1e-;
struct Trie{
int v;
Trie *next[];
};
Trie root; void creat(char *str) {
int len=strlen(str);
Trie *p=&root, *q;
for (int i=; i<len; i++) {
int id=str[i]-'a';
if (p->next[id]==NULL) {
q=(Trie *)malloc(sizeof(root));
q->v=;
for (int j=; j<; j++)
q->next[j]=NULL;
p->next[id]=q;
p=p->next[id];
}
else {
p->next[id]->v++;
p=p->next[id];
}
}
}
int Find(char *str) {
int len=strlen(str);
Trie *p=&root;
for (int i=; i<len; i++) {
int id=str[i]-'a';
p=p->next[id];
if (p==NULL) return ;
}
return p->v;
}
void solve() {
char str[];
for (int i=; i<; i++)
root.next[i]=NULL;
while(gets(str)) {
if (str[]=='\0') break;
creat(str);
}
while(gets(str)) {
if (str[]=='\0') break;
int ans=Find(str);
printf("%d\n", ans);
}
}
int main() {
int t = ;
// freopen("in.txt", "r", stdin);
scanf("%d", &t);
while(t--)
solve();
return ;
}

哈希与字典树与KMP的更多相关文章

  1. poj 2503 哈希 Map 字典树

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36967   Accepted: 15749 Descr ...

  2. 字典树应用 - poj1002

    字典树应用 - poj 1002 Description Businesses like to have memorable telephone numbers. One way to make a ...

  3. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  4. 踹树(Trie 字典树)

    Trie 字典树 ~~ 比 KMP 简单多了,无脑子选手学不会KMP,不会结论题~~ 自己懒得造图了OI WIKI 真棒 字典树大概长这么个亚子 呕吼真棒 就是将读进去的字符串根据当前的字符是什么和所 ...

  5. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  6. Colored Sticks (字典树哈希+并查集+欧拉路)

    Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27704   Accepted: 7336 Description You ...

  7. 数据结构图解(递归,二分,AVL,红黑树,伸展树,哈希表,字典树,B树,B+树)

    递归反转 二分查找 AVL树 AVL简单的理解,如图所示,底部节点为1,不断往上到根节点,数字不断累加. 观察每个节点数字,随意选个节点A,会发现A节点的左子树节点或右子树节点末尾,数到A节点距离之差 ...

  8. Watto and Mechanism CodeForces - 514C (字典树,哈希)

    大意: 给定字符串集$S$, 每次询问给出字符串$a$, 求$S$中是否存在一个字符串恰好与$a$相差一个字符. 直接建字典树暴力复杂度是$O(n\sqrt{n})$, 也可以用set维护所有哈希值, ...

  9. 用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成“***”就可 ...

随机推荐

  1. .net 中使用oracle 的sql 语句

    string sqlString = "Select * From emp  Where EMPNO=7369“; 一定不要写成 string sqlString = "Selec ...

  2. 获取txt里面的内容

    import flash.net.URLLoader; import flash.net.URLRequest; import flash.events.Event; var txtLoad:URLL ...

  3. CentOS 下搭建Tomcat

    1.下载tomcat软件包 wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.ta ...

  4. 第一个springMVC小程序

    1首先配置一个前端控制器,在web.xml文件中配置(dispatcherservlet) <!-- 前端控制器配置 --> <servlet> <servlet-nam ...

  5. css定位研究

    css的定位是很重要的一个知识点,要学会网页布局,一定要先把定位弄清楚,今天抽空整理一下这方面的知识. 1.块级元素和行内元素(内联元素) 块级元素:display值为block的元素就是块级元素,比 ...

  6. lombok ------让代码更简洁方便

    估计在平常写代码中,都会创建entity类的实体来,都是那种创建变量,生成set get 方法,方便外部调用,你以为你很流利的操作快捷键就很方便的了? 其实不然,有一个lombok 工具可以帮我们自动 ...

  7. 配置eclipse+SDK+ADT开发环境

    第一步:配置jdk和eclipse环境(已完成): 第二步:ADT配置:依次点击菜单栏:help -> Install new software -> Add -> Local... ...

  8. 利用jenkins+saltstack+sh 修改nginx配置文件并重新加载

    jenkins的配置(这里作用只是当做界面使用,利用它来管理执行salt命令) 1.构建操作来执行shell脚本 (pillar可以配置灵活的参数) saltstack 的 sls文件编写 nginx ...

  9. A Spectral Technique for Correspondence Problems Using Pairwise Constraints

    Abstract 我们提出了一种有效的谱方法来寻找两组特征之间的一致对应关系.我们建立了一个图的邻接矩阵M,它的节点代表了潜在的对应,而链接上的权重代表潜在的对应之间的成对协议.正确的分配可在彼此之间 ...

  10. Web框架Danjgo之session cookie及认证组件

    一 Cookie 1 什么是Cookie Cookie翻译成中文是小饼干的意思.其实Cookie是key-value结构,类似于一个Python中的字典.随着服务器端的响应发送给客户端浏览器. 然后客 ...