hash + segment tree HDU 3973 AC's String

This problem is solved using Hash of strings. Consider a string as a P-base number (you can associate a decimal number), then you can know that each string can be uniquely represented (P> 256, regardless of high precision). However, due to the large length of the string, we cannot store such a large number. So use Hash mod2^64 as Hash.(It may conflict, but the probability of conflict tends to be infinitely small)
Then Hash(s,P) = s[0]*P^(Len-1) + s[1 ] *P^(Len -2) + … + s[Len-1]*P^0 (mod2^64)
P can choose any prime number or whatever.
Assuming that there is no modification operation, then we can process the H[i] to represent the Hash of S[0..i]
Then in order to obtain the Hash of S[l..r], we have:
S[0..r]= s[0]*P^r + s[1]*P^(r-1) +…+s[r]*P^0
S[0..l-1]=s[0]*P^( l-1)+s[1]*P^(l-2)+…+s[l-1]*P^0
So
S[l..r]=s[l]*P^(rl)+ s[l+1]*P^(rl-1)+…+s[r]*P^0
=S[0..r] – S[0..l-1]*P^(r-l +1)
=H[r] – H[l-1]*P^(r-l+1) 
where P^(r-l+1) is obviously preprocessed directly.
So the query can be O(1)
and now some characters need to be modified!
So you can use a line segment tree to maintain

[L..R] maintains the Hash of S[L..R], so that update queries are resolved. Both update and query are O(logN) level

Reprinted from https://www.2cto.com/kf/201209/153293.html

Code https://blog.csdn.net/acm_baihuzi/article/details/48957801


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325805558&siteId=291194637