This commit is contained in:
Zengtudor 2025-08-20 19:59:10 +08:00
parent 3204ce5c1e
commit f41c1341a0

36
src/8/20/hash.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <algorithm>
#include <cstdint>
#include <ios>
#include <string>
#include <vector>
#include <iostream>
using ull = uint64_t;
template<ull P=47,ull maxsize=(ull)1e3>
struct Hash{
ull hash{};
std::vector<ull> powp;
Hash(const std::string&s):powp(maxsize){
ull npp=P;
powp[0]=1;
powp[1]=npp;
for(ull i=2;i<maxsize;i++){
npp*=npp;
powp[i]=npp;
}
ull k=0;
for(auto it=s.rbegin();it!=s.rend();++it){
hash+=((*it)-'A'+1)*powp[k];
}
}
bool operator==(const Hash<P,maxsize>&o)const{
return hash==o.hash;
}
};
#define pv(v)do{std::cout<<std::boolalpha<<#v<<" :"<<(v)<<'\n';}while(0)
int main(){
pv(Hash("aaaaaaaa")==Hash("aaaaaaa"));
}