字符串的hash匹配
如果要比较字符串是否相等,首先想到的是KMP算法,但是hash更加简洁易于编写,hash的目的是把一串字符转化成一个数字,用数字来比较是否相等。我让mod=912837417 Base=127,防止hash碰撞
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod=;
const LL Base=;
char T[],P[];
LL lenT,lenP;
LL hash[];
LL base[];
LL hashP;
inline LL query(LL l,LL r){
LL ans=(hash[r]-(hash[l-]*base[r-l+]))%mod;
if(ans<) ans+=mod;
return ans;
}
int main(){
scanf("%s%s",T+,P+);
lenT=strlen(T+); lenP=strlen(P+);
base[]=;
for(int i=;i<=lenT;i++){
base[i]=(base[i-]*Base)%mod;
hash[i]=(hash[i-]*Base+(LL)(T[i]-'a'+))%mod;
if(i<=lenP) hashP=(hashP*Base+(LL)(P[i]-'a'+))%mod;
}
for(int i=;i<=lenT-lenP+;i++){
LL now=query(i,i+lenP-);
if(now==hashP){
cout<<"从第"<<i<<"位开始匹配"<<endl;
return ;
}
}
cout<<"不能匹配"<<endl;
return ;
}
字符串的hash匹配的更多相关文章
- OJ——华为编程题目:输入字符串括号是否匹配
package t0815; /* * 华为编程题目:输入字符串括号是否匹配 * 若都匹配输出为0,否则为1 * 样例输入:Terminal user [name | number (1)] * 样例 ...
- redis 2 字符串 和 hash
string是最简单的类型,一个key对应一个value,string类型是二进制安全的.redis的string可以包含任何数据,比如JPG图片或者序列化的对象 操作 set 设置key ...
- HDU 2585 Hotel(字符串的模糊匹配+递归)
Problem Description Last year summer Max traveled to California for his vacation. He had a great tim ...
- Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法
1. 使用find()方法 >>> text = 'yeah, but no, but yeah, but no, but yeah' >>> text.find( ...
- mycat1.6.5分片(字符串拆分hash)
https://blog.csdn.net/webnum/article/details/78313525 分片规则:字符串拆分hash 一.conf/schema.xml文件 <?xm ...
- 解决:Oracle 异常 ORA-01861: literal does not match format string(字符串格式不匹配)
今天在写存储过程的时候,老是报一个错.如下,记录下来,以供日后查阅. 报错: Oracle 异常 ORA-01861: literal does not match format string(字符 ...
- LeetCode10 Hard,带你实现字符串的正则匹配
本文始发于个人公众号:TechFlow 这是LeetCode的第10题,题目关于字符串的正则匹配,我们先来看题目相关信息: Link Regular Expression Matching Diffi ...
- 字符串算法hash
思路:给字符串做一个映射,两个元素相同,则他们的hash值必定相同. 注意:hash表必须是unsigned int类型,保证每个映射都是正数. 例题: Description 给出两个字符串W和T, ...
- REGEXP 正则的实现两个字符串组的匹配。(regexp)
主要懂3个mysql的方法:replace[替换] regexp[正则匹配] concat[连接] 由于某些原因,有时候我们没有按照范式的设计准则而把一些属性放到同一个字符串字段中.比如 ...
随机推荐
- CentOS 同步时间
来源:http://www.ctusky.com/16/0497/ 用date查看系统当前时间,date -R 可查看时区. CentOS 同步时间由ntp服务提供,可以用"yum inst ...
- mongodb在32位机的连接
Windows 32bit版本安装Mongodb时,会发生的下面问题 2016-05-09T00:09:45.124+0800 I STORAGE [initandlisten] exception ...
- shared_ptr & weak_ptr
shared_ptr <1> 类模板说明 namespace boost { class bad_weak_ptr: public std::exception; template< ...
- [LintCode] 最后一个单词的长度
class Solution { public: /** * @param s A string * @return the length of last word */ int lengthOfLa ...
- Apache-ActiveMQ transport XmlMessage
前言 本文原创,转载请说明出处! Apache-ActiveMQ:是Apache提供的一款开源的消息传送服务,不论是安装还是使用都非常简单实用,可以作为消息缓存管理器来使用(个人观点),看看Activ ...
- 【论文翻译】MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications 论文链接:https://arxi ...
- Casperjs中fill提交表单遇到的问题
1.if you access internet with proxy please add --ignore-ssl-errors=true --ssl-protocol=a ...
- Java栈(Stack)和堆(Heap)
In the following code public void Method1() { int i = 4; int y = 2; class1 cls1 = new class1(); } He ...
- 模块 - time/datetime
time 模块 time模块方法: >>> import time >>> time.time() #时间戳 秒级别 1519212085.6211221 #从19 ...
- 文件操作 - 三元运算/chardet/文件操作r w/文件的操作方法
Alex:读书可以改变一个人的气质读书:豆瓣: 1年读20本 你的问题:想法太多,读书太少 书:追风筝的人,白鹿原 电影:阿甘正传 辛德勒名单---------------------------- ...