Answer the question
In order to leave comments, you need to log in
Why doesn't hashes work?
wrote a hash function but gethash gives wrong value here is the code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll p = 400;
ll M = 1000000007;
void powof(ll x, vector<ll>&powx){
powx[0] = 1;
ll z = x;
for(int i = 1; i < powx.size(); i++){
powx[i] = x;
x = x * z % M;
}
}
ll gethash(ll l, ll r, vector<ll> prefhash, vector<ll> powp){
if(l == 0) return prefhash[r];
return abs(prefhash[r] - prefhash[l-1] * powp[r - l + 1]% M + M) % M;
}
ll xhash(string s, ll l, ll r){
ll hash = long (s[l]);
if(l == r) return long (s[l]);
for(int i = l; i < r + 1; i++){
hash = (hash * p + int(s[i])) % M;
}
return hash;
}
int main() {
string s1,s2;
ll hs2;
cin >> s1 >> s2;
vector<ll> hs1(s1.size());
vector<ll> powp(s1.size() + 1);
for(int i = 0; i < s1.size(); i++){
hs1[i] = xhash(s1,0,i);
}
hs2 = xhash(s2,0,s2.size()-1);
powof(p,powp);
for(auto u : hs1){
cout << u << " " ;
}
cout << endl;
for(auto u : powp){
cout << u << " ";
}
}
Answer the question
In order to leave comments, you need to log in
Well, at least write what kind of hashes you think here. It looks like you have a polynomial hash.
Both xhash and gethash are misspelled.
Let's look at xhash. If l==r, then s[l]*1 will be returned. Seems right. If l+1=r, then your function will return s[l]*p^2+s[l]*p+s[r]. It is unlikely that this is what you wanted to achieve. Either the initialization before the loop is incorrect, or the loop has boundaries.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question