哈希与字典树与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算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成“***”就可 ...
随机推荐
- mysql 5.7.21 主从集群恢复GTID方式(不锁库)
从库损坏后,进行恢复 1.查看主加标记点 show master status\G 记录下POST的值 2.备注主库数据 mysqldump -u root -p -S /data/mysql/mys ...
- VM 端口映射问题
环境: 宿主机:WIN 10 --192.168.9.87 虚拟机:CentOS ---192.168.255.129 联通模式:NAT模式 一.VM做端口映射 1.为了方便管理,设置虚拟机为静态IP ...
- 江西财经大学第一届程序设计竞赛 F题 解方程
链接:https://www.nowcoder.com/acm/contest/115/F来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- spring上下文快速获取方法
import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContex ...
- 安装routeos
直接开机,会看到: 选择全部安装即可,按a.i即可. 默认账号admin,默认没有密码 基本使用 可通过/ip,/interface等可以进去不同子功能模块,可进行print,add,remove等操 ...
- LAB3 整数相加
//yuec2 Yue Cheng package lab3; public class Fraction { int numerator; int denominator; //obeject wi ...
- 管道分隔符Split
string[] areaID = area1Id.Split(new char[] { ',' });
- 5-Error:failed to find Build Tools revision 28.0.0 rc1解决方案
将app下面的build.gradle中的版本改为你安装的 sdk 版本:
- Hibernate: save, persist, update, merge, saveOrUpdate[z]
[z]https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate 1. Introduction In this ...
- c# 使用ssh.net 上传文件
在ssh.net 客户端实例下无法普通用户切换到su root 超级用户,原因是tty 的不支持,具体原因未查, 连接时用超级用户,问题解决 使用ssh.net 能实现远程命令, 使用其中的sf ...