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