Hackerrank-the Grid Search

Source: Internet
Author: User

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

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.