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. 203. Remove Linked List Elements (List)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  2. RxJS之过滤操作符 ( Angular环境 )

    一 take操作符 只发出源 Observable 最初发出的的N个值 (N = count). 如果源发出值的数量小于 count 的话,那么它的所有值都将发出.然后它便完成,无论源 Observa ...

  3. textarea 自动到右边就换行

    java 到最右边的时候自动换行如实例: textArea.setLineWrap(true);

  4. Django具体操作(六)

    文章详情页的编写: {% extends "base.html" %} {% block content %} {% csrf_token %} <div class=&qu ...

  5. GridView中CheckBox单击事件(oncheckedchanged)

    在GridView中加入 CheckBox控件,想通过单击选中出现如下图所示效果: 具体做法是: 前台GV部份省掉.只加关键的CheckBox部份. view plaincopy to clipboa ...

  6. day 06 列表去重, 数据类型的补充,编码,深浅copy

    因为重要,所以放前面 列表去重 l1 = [1, 2, 3, 4, 5] l2 = [3, 4, 5, 6, 7] set = list(set(l1 + l2)) # set自动去重,然后变成lis ...

  7. 关于MYSQL字符集问题(一)

    MySQL的字符集支持(Character Set Support)有两个方面: 字符集(Character set)和排序方式(Collation). 对于字符集的支持细化到四个层次: 服务器(se ...

  8. 编程,将data段中的字符串转化成大写

    assume cs:code data segment db 'conversation' data ends code segment start: mov ax,data mov ds,ax ca ...

  9. python3 安装使用 fabirc3 模块以及 fab 命令(转)

    原文地址:https://blog.csdn.net/cityzenoldwang/article/details/78454964 python3 fabric3 模块之 fab 命令 安装 pyt ...

  10. rbac集成 权限分配。之用户管理

    流程都是一样的.就不在详细的记录.只写一点需要注意的地方! 或者 可以改进的地方! 1. 用户表中 只有. name  password email 三个字段. 但是添加用户的页面,应该还要有确认密码 ...