UVa 12206 (string hash) stammering Aliens

Source: Internet
Author: User

Experience a string hash of the practice, feel the hash of this personality algorithm is very magical.

Perhaps the positive solution of this problem is a suffix array, but the advantage of hashing is that the complexity of coding is greatly reduced.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 Const intMAXN =40000+Ten;7 Const intx =123;8 intN, M, POS;9UnsignedLong LongH[MAXN], XP[MAXN];TenUnsignedLong LongHASH[MAXN]; One intRANK[MAXN]; A CharS[MAXN]; -  - BOOLcmpConst int& A,Const int&b) the{returnHash[a] < Hash[b] | | (Hash[a] = = Hash[b] && a <b); } -  - BOOLPossibleintL) - { +     intCNT =0; -pos =-1; +      for(inti =0; i + L-1< n; i++) A     { atRank[i] =i; -Hash[i] = h[i]-h[i + L] *Xp[l]; -     } -Sort (rank, rank + n-l +1, CMP); -      for(inti =0; i + L-1< n; i++) -     { in         if(i = =0|| Hash[rank[i]]! = hash[rank[i-1]]) CNT =0; -         if(++cnt >= m) pos =Max (POS, rank[i]); to     } +     returnPOS >=0; - } the  * intMain () $ {Panax Notoginseng     //freopen ("In.txt", "R", stdin); -  thexp[0] =1; +      for(inti =1; i < MAXN; i++) Xp[i] = xp[i-1] *x; A  the      while(SCANF ("%d", &m) = =1&&m) +     { -scanf"%s", s); $n =strlen (s); $H[n] =0; -          for(inti = n-1; I >=0; i--) H[i] = H[i +1] * x + s[i]-'a'; -  the         if(!possible (1)) puts ("None"); -         ElseWuyi         { the             intL =1, R =N; -              while(L <R) Wu             { -                 intM = (L + R +1) /2; About                 if(Possible (M)) L =M; $                 ElseR = M-1; -             } - Possible (L); -printf"%d%d\n", L, POS); A         } +     } the  -     return 0; $}
code June

UVa 12206 (string hash) stammering Aliens

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.