哈希与字典树与KMP
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的更多相关文章
- poj 2503 哈希 Map 字典树
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36967 Accepted: 15749 Descr ...
- 字典树应用 - poj1002
字典树应用 - poj 1002 Description Businesses like to have memorable telephone numbers. One way to make a ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 踹树(Trie 字典树)
Trie 字典树 ~~ 比 KMP 简单多了,无脑子选手学不会KMP,不会结论题~~ 自己懒得造图了OI WIKI 真棒 字典树大概长这么个亚子 呕吼真棒 就是将读进去的字符串根据当前的字符是什么和所 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- Colored Sticks (字典树哈希+并查集+欧拉路)
Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27704 Accepted: 7336 Description You ...
- 数据结构图解(递归,二分,AVL,红黑树,伸展树,哈希表,字典树,B树,B+树)
递归反转 二分查找 AVL树 AVL简单的理解,如图所示,底部节点为1,不断往上到根节点,数字不断累加. 观察每个节点数字,随意选个节点A,会发现A节点的左子树节点或右子树节点末尾,数到A节点距离之差 ...
- Watto and Mechanism CodeForces - 514C (字典树,哈希)
大意: 给定字符串集$S$, 每次询问给出字符串$a$, 求$S$中是否存在一个字符串恰好与$a$相差一个字符. 直接建字典树暴力复杂度是$O(n\sqrt{n})$, 也可以用set维护所有哈希值, ...
- 用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成“***”就可 ...
随机推荐
- 2019年华南理工大学程序设计竞赛(春季赛)-C-六学家的困惑
题目链接:https://ac.nowcoder.com/acm/contest/625/C 题意:给定两个字符串,每次只能从两个字符串的两端取字符,求依次取字符后所构成的数字最大为多少. 思路:思路 ...
- x86寄存器总结
X86寄存器 ·x86寄存器分类: 8个通用寄存器:EAX.EBX.ECX.EDX.ESI.EDI.ESP.EBP 1个标志寄存器:EFLAGS 6个段寄存器:CS.DS.ES.FS.GS.SS 5个 ...
- oracle视图(转)
视图的概念 视图是基于一张表或多张表或另外一个视图的逻辑表.视图不同于表,视图本身不包含任何数据.表是实际独立存在的实体,是用于存储数据的基本结构.而视图只是一种定义,对应一个查询语句.视图的数据 ...
- 【转】web.xml配置项详解
史上最全web.xml配置文件元素详解 一.web.xml配置文件常用元素及其意义预览 1 <web-app> 2 3 <!--定义了WEB应用的名字--> 4 <d ...
- 转:WEB前端性能优化规则
14条规则摘自<High Performance Web Sites>,本文地址 1.减少Http请求 使用图片地图 使用CSS Sprites 合并JS和CSS文件 这个是由于浏览器对同 ...
- TZOJ 2648 小希的迷宫(并查集)
描述 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道 ...
- web项目no such method exception
昨天更新包后出现这个异常,经过仔细全面排查,项目源码是没问题的. 怀疑jvm被重写了,肉眼也没找到证据.怀疑是操作系统问题,这个也不会没办法排查 于是给客户重新发了个war包,客户运行后出现 异常: ...
- check-versions.js和dev-client.js
// 用于在控制台输出带颜色字体的插件var chalk = require('chalk') // 语义化版本检查插件(The semantic version parser used by npm ...
- [z]一步步教你如何在 Visual Studio 2013 上使用 Github
[z]http://www.admin10000.com/document/4004.html 介绍 我承认越是能将事情变简单的工具我越会更多地使用它.尽管我已经知道了足够的命令来使用Github,但 ...
- 自定义 Mysql 类 与 自定义 异常类
import MySQLdb class MyExcept(Exception): ''' 常见做法定义异常基类,然后在派生不同类型的异常 ''' def __init__(self, *args): ...