Data Structures & AlgorithmsHardGATE CS · ICPC regional
A polynomial hash is H(s) = (Σ ord(s[i]) · B^(n−1−i)) mod M, computed left to right as h = (h·B + ord(c)) mod M, with B = 31 and M = 1000003. What is H("dsa")?
- A. 96882
- B. 99762
- C. 99763
- D. 312
Correct answer: B. 99762
Horner's rule computes the polynomial in one pass: each character multiplies the running hash by B then adds its code. Reversing the string changes which character gets the highest power, so the hash of the reversed string is a different number.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many DISTINCT palindromic substrings (by content, length at least 1) does "abaaba" have?
- Let t = "ABCC" (its letters may repeat). What is the length of the shortest substring of "ADOBECODEBANCXY" that contains every letter of t…
- Among all cyclic rotations of "bcabca" (there are n = 6 of them, including the original), what is the 0-based index i of the…
- What is the minimum number of characters that must be inserted at the FRONT of "abaccabb" to make the whole string a palindrome?
- How many contiguous subarrays of [3, 4, -7, 1, 3, 3, 1, -4, 2] have sum exactly 7? Negative numbers are present.
- In "swissmississippi", what is the 0-based index of the first character that appears exactly once in the whole string?