HDU 3973 AC ' s string string hash

Source: Internet
Author: User

HDU 3973

By converting a string into an integer through a hash function, it is possible to make the hash value virtually non-conflicting (this is the key point, almost no = =!). , you can also consider the process of hashing conflicts, but this problem is not necessary, and then use the segment tree to maintain the modified hash value.

Because the input string is only 26, consider using a prime number greater than or equal to 26 as the binary, then convert the original string into a P-binary mod 2^63 (also equivalent to a natural overflow), then this number is stored in the map, and then use the segment tree to maintain the hash value of the long range, Hash[l, R] Represents the result of converting a string of intervals [L, R] to a P-binary number (hash procedure), then for the two sub-bands [L, M] and [M + 1, R] of the current interval [L, R]:

Hash[l, R] = Hash[l, M] + hash[m + 1, r] * (p ^ (m-l + 1))

Using the Segment tree dynamic maintenance, query interval hash value, using map lookup, then the problem is solved.

1 //#pragma COMMENT (linker, "/stack:1677721600")2#include <map>3#include <Set>4#include <stack>5#include <queue>6#include <cmath>7#include <ctime>8#include <vector>9#include <cstdio>Ten#include <cctype> One#include <cstring> A#include <cstdlib> -#include <iostream> -#include <algorithm> the using namespacestd; - #defineINF 0x3f3f3f3f - #defineInf (-((LL) 1<<40)) - #defineLson k<<1, L, (L + R) >>1 + #defineRson k<<1|1, ((L + R) >>1) + 1, R - #defineMem0 (a) memset (A,0,sizeof (a)) + #defineMem1 (a) memset (A,-1,sizeof (a)) A #defineMem (A, B) memset (A, B, sizeof (a)) at #defineFIN freopen ("In.txt", "R", stdin) - #defineFOUT freopen ("OUT.txt", "w", stdout) - #defineRep (I, A, b) for (int i = A; I <= B; i + +) - #defineDec (i, A, b) for (int i = A; I >= b; i-) -  -template<classT> T MAX (t A, T b) {returna > B?a:b;} intemplate<classT> T MIN (t A, T b) {returnA < b?a:b;} -template<classT> T GCD (t A, T b) {returnB? GCD (b, a%b): A; } totemplate<classT> T LCM (t A, T b) {returnA/GCD (A, b) *b; } +  - //typedef __int64 LL; thetypedefLong LongLL; * Const intMAXN =100000+ -; $ Const intMAXM =110000;Panax Notoginseng Const DoubleEPS = 1e-8; -LL MOD =1000000007; the  + ConstLL P = -; A  the intT, N, m, cas =0, L, R; + LL MI[MAXN]; - Charstr[2000001], ch; $Map<ll,int>MP; $  - voidinit () { -mi[0] =1; theRep (I,1, MAXN-1) { -Mi[i] = mi[i-1] *P;Wuyi     } the } -  Wu intTonum (Charch) { -     returnCH-'a'+1; About } $  - LL Hash () { -LL ans =0; -     intLen =strlen (str); ADec (i, Len-1,0) { +Ans = ans * P +Tonum (Str[i]); the     } -     returnans; $ } the  the structSegtree { theLL MA[MAXN <<2]; the  -     voidUpdateintKintLintRintPintv) { in         if(L = = R) {Ma[k] = V;return ; } the         intMid = (L + R) >>1; the         if(Mid >=p) Update (Lson, p, v); About         ElseUpdate (Rson, p, v); theMa[k] = ma[k <<1] + ma[k <<1|1] * mi[mid-l +1]; the     } the  +LL Query (intKintLintRintLintr) { -         if(R < L | | R < L)return 0; the         if(l <= L && R <= R)returnMa[k];BayiLL le = Query (Lson, L, r), RI =query (Rson, L, R); the         intMid = (L + R) >>1; the         returnLe + ri * Mi[max (Mid-max (L, L) +1,0)]; -     } -  the }st; the  the intMain () the { - //FIN; the init (); theCIN >>T; the      while(t--) {94 mp.clear (); the mem0 (st.ma); thescanf"%d%*c", &n); theRep (I,0N1) {98scanf"%s", str); AboutMp[hash ()] =1; -         }101scanf"%s", str);102         intLen =strlen (str);103Rep (I,0, Len-1) {104St.update (1,1, Len, i +1, Tonum (Str[i])); the         }106printf"Case #%d:\n", ++CAs);107scanf"%d%*c", &m);108          while(M--) {109scanf"%c", &ch); the             if(ch = ='Q') {111scanf"%d%d%*c", &l, &R); theprintf"%s\n", Mp[st.query (1,1, Len, ++l, ++r)]?"Yes":"No");113             } the             Else { thescanf"%d%c%*c", &l, &ch); theSt.update (1,1, Len, + +l, Tonum (ch));117             }118         }119     } -     return 0;121}

HDU 3973 AC ' s string string hash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.