字符串hash总结
来源:互联网 发布:看演唱会软件 编辑:程序博客网 时间:2024/05/22 01:52
/*
洛谷模板题(哈希表)
#include<iostream>
#include<cstdio>#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1510;
const int mod=9999983;
const int base=131;
char s[maxn];
vector<int> g[mod];
long long num=0,ans=0;
void hash(){
num=0;
for(int i=0;i<strlen(s);i++){
num=(num*base+s[i]-'0')%mod;
}
}
void push(){
long long cur=num%mod;
for(int i=0;i<g[cur].size();i++){
if(g[num][i]==num){
return ;
}
}
g[cur].push_back(num);
++ans;
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s);
hash();
push();
}
cout<<ans<<endl;
return 0;
}*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod=1000003;
const int base=131;
const int maxn=1510;
int f[maxn],d[maxn];
char s[maxn];
ll hash(int l,int r){
long long a=f[r];
ll b=f[l-1]%mod*d[r-l+1]%mod;
b%=mod;
return (a-b+mod)%mod;
}
int main(){
scanf("%s",s+1);
d[0]=1;
for(int i=1;i<strlen(s+1);i++){
f[i]=(f[i-1]*base+s[i]-'a')%mod;
d[i]=d[i-1]*base%mod;
}
cout<<hash(2,4);
return 0;
}
阅读全文
0 0
- 字符串Hash总结
- 字符串hash总结
- 常用字符串hash函数总结
- 字符串HASH 学习总结 && 模板
- 关于字符串hash函数的总结
- 字符串hash,康托展开总结
- 字符串hash
- 字符串HASH
- 字符串Hash
- 字符串hash
- 字符串hash
- 字符串hash
- 字符串Hash
- 字符串hash
- 字符串hash
- 字符串 HASH
- 字符串Hash
- 字符串hash
- ZOJ 3981 Balloon Robot(提前位置取消等待时间)
- android 百度地图根据地址检索经纬度
- solr集群搭建
- android.view.WindowManager$BadTokenException崩溃分类与解决。
- Oracle中的包、触发器 以及索引
- 字符串hash总结
- 你眼中的Python大牛,应该都看过这些书
- 虚拟地址空间&虚拟内存&物理内存
- nginx代理wss
- 图像的复原与重建(3):图像去噪(中值、均值、谐波均值、自适应、带通、带阻、陷波滤波器)
- day3上午模拟赛
- 怎么用IDEA上传本地项目到码云/Github?
- caffe 容易犯的错误
- 这 8 份「Paper + Code」,你一定用得上 | PaperDaily #08