Https://www.hackerrank.com/challenges/the-grid-search/forum
Today, I met this problem, see the difficulty is moderate, think should be able to finish within half an hour.
After reading the topic found is purely a sub-matrix matching problem , think of oneself have not done before, certainly can learn the new algorithm, then opened up.
So the internet searched the Rabin-karp algorithm, a pattern matching algorithm based on hashing. Although I did not write one-dimensional, but after looking at the idea of the promotion to the two-dimensional should not be difficult.
So the following code, the principle is to calculate the sub-matrix hash key. The comparison of the sub-matrices is replaced by the hash key, so that the sub-matrix of the hash key is excluded first.
For the hash key equal, then the naïve method to determine whether the sub-matrix is equal.
Why do we finally have to decide whether the sub-matrices are equal? Because the hash key may have a collision, even if the probability is small, in order to ensure correctness also need to check.
Learn the RABIN-KARP algorithm information in this:
Http://blog.sina.com.cn/s/blog_6a09b5a70100nhnr.html
Although the idea is simple, the code is written but a variety of bugs, and finally I spent no less than two head to fix. Hackerrank really is to play for hacker, I this level on the above is very difficult.
However, this is a real harvest, if the learning algorithm can keep the rhythm of the good.
The following is the code for AC, where the space-time complexity is O (N ^ 2):
1 #2D Rabin-karp Algorithm2 ImportRe3 4MOD = 10 * * 9 + 75 6 defGet2dmatrix (N, m):7a = [[0] forJinchXrange (m)] forIinchxrange (n)]8 returna9 Ten defCalchash (A, nn, mm): Onen =Len (a) Am =Len (a[0]) - -b = 1 the forIinchxrange (mm): -b = b * 10%MOD -b2 = 1 - forIinchxrange (NN): +b2 = B2 * B%MOD - +h =Get2dmatrix (n, m) A forIinchxrange (n): atval =0 - forJinchxrange (m): -val = (val * + a[i][j])%MOD - ifJ >=mm: -val = (val + a[i][j-mm] * (mod-b))%MOD -H[I][J] =Val in -H2 =Get2dmatrix (n, m) toH2[0] =h[0][:] + forIinchXrange (1, N): - forJinchxrange (m): theH2[I][J] = (H2[i-1][j] * b + h[i][j])%MOD * ifI >=nn: $H2[I][J] = (H2[i][j] + h[i-nn][j] * (MOD-B2))%MODPanax Notoginseng returnh, H2 - the defequal (A, p, Ai, AJ): +NP =len (P) AMP =Len (p[0]) the forIinchxrange (NP): + forJinchXrange (MP): - ifA[ai + I][aj + j]! =P[i][j]: $ returnFalse $ returnTrue - - defsolve (): theNa, MA = map (int, re.split ('\s+', Raw_input (). Strip ())) -A = []Wuyi forIinchxrange (NA): the a.append (Map (int, list (raw_input (). Strip () ))) -NP, MP = map (int, re.split ('\s+', Raw_input (). Strip ())) Wup = [] - forIinchxrange (NP): About p.append (Map (int, list (raw_input (). Strip () ))) $Ha, H2A =Calchash (A, NP, MP) -HP, h2p =Calchash (P, NP, MP) - - forIinchXrange (np-1, NA): A forJinchXrange (mp-1, MA): + ifH2A[I][J]! = h2p[np-1][mp-1]: the Continue - ifEqual (A, p, I-NP + 1, J-MP + 1): $ Print('YES') the return the Print('NO') the the if __name__=='__main__': -t =Int (raw_input ()) in forTiinchxrange (t): the Solve () the
Hackerrank-the Grid Search