iTranslated by AI
Learning Rolling Hash through University of Tokyo Graduate School Entrance Exams
I will introduce the Rolling Hash algorithm[1] used in string searching, based on an entrance exam question for the University of Tokyo's Graduate School (Graduate School of Information Science and Technology, Department of Computer Science, FY2020 Specialized Subject I, Question 3).
The PDF of the problem is available below. [2]
Although this problem is themed around Rolling Hash, it is designed to be solvable even if you don't know the algorithm beforehand; rather, you can understand Rolling Hash through this problem. In that sense, it is a very educational question and, I believe, a beneficial one for those who want to learn about Rolling Hash from now on. [3]
Now, let's look at the problem.
Problem Statement [1]
Many symbols have appeared. Let's organize them.
Regarding string
Length of
The
The string obtained by removing the first
Regarding character
A function that maps
(For example, you can imagine character codes.)
Also, let's read the description of the FIND problem. This is an explanation of string searching. For example, if
PROBLEM
OBL
No match
PROBLEM
OBL
No match
PROBLEM
OBL
Match!
In the case of a match,
and
If
Problem Statement [2]
Now, an algorithm S to solve the FIND problem has been presented. Let's think about what this algorithm is doing.
Inside the for-loop, a function called eq(s+i, p) is called every time until it returns 1. Reading the problem statement, we can see that eq(s+i, p) is a function that returns 1 if the first
Here,
Compare the string obtained by removing the first
Compare the string obtained by removing the first
Compare the string obtained by removing the first
We can see that this is the same brute-force search as before:
PROBLEM
OBL
No match
PROBLEM
OBL
No match
PROBLEM
OBL
Match!
Problem Statement [3]
(1) is a problem to find the worst-case computational complexity of algorithm S. The most time-consuming case is, of course, when
From the beginning of the problem statement, one string comparison eq(s+i, p) takes
(I feel it's also acceptable to write it as
Problem Statement [4]
Finally, the hash appeared.
It might be hard to understand the original formula just by looking at it, so let's first think about it with a concrete example. Let
To make it easier to visualize, let's consider the case where
For example, if we set
Right?
Problem Statement [5]
(2) asks how to find
Looking back at the initial settings in the problem statement, the operations that can be done in
- The operation to find
froms+i ands i - The operation to find
fromnumval(c) c - Addition, multiplication, and modulo operations of integers
Now, let's look back at the concrete example from earlier.
From here,
Furthermore, since
By this point, you might notice that the form of
Since addition, subtraction, and multiplication are closed under modulo[4], to find
- Subtract
fromnumval(s[0]) \cdot d^3 ,h(s, 4) - Multiply it by
,d - Add
to it,numval(s[4]) - And finally take
\mod q
This operation works.
For the general case:
You can find
- Subtract
fromnumval(s[i]) \cdot d^{m-1} = numval(s[i]) \cdot d_m ,h' = h(s + i, m) - Multiply it by
,d - Add
to it,numval(s[i+m]) - And finally take
\mod q
In pseudo-code, it would look like this:
tmp = numval(s[i]);
tmp = tmp * d_m;
ans = h' + tmp * (-1);
ans = ans * d;
ans = ans + numval(s[i+m]);
ans = ans % q;
return ans;
Since numval(), +, *, and % can be done in
Problem Statement [6]
Now, (3) is a problem about considering the string search algorithm using rolling hash, which is the main topic. Note that two questions are given.
First, regarding the first half of the problem, let's think about using the hint from (2).
Therefore, a pseudo-code for an algorithm to find the smallest non-negative integer
Precompute h(p, l(p))
Precompute h(s, l(p))
Precompute d_l(p)
if (h(p, l(p)) == h(s, l(p)))
return 0;
for (i = 0; i <= l(s)-l(p)-1; i++)
Calculate h(s+i+1, l(p)) in O(1) from h(s+i, l(p)) and d_l(p)
if (h(p, l(p)) == h(s+i+1, l(p)))
return i+1;
return -1;
Since hashes are just integer values, comparing hashes can naturally be done in
What remains is to analyze the complexity of these parts:
- Precomputing
h(p, l(p)) - Precomputing
h(s, l(p)) - Precomputing
d_{l(p)} = d^{l(p) - 1}
First, since multiplication can be done in
Also, the hash of a string of
ans = numval(s[0]);
for (i = 1; i <= l(p)-1; i++)
ans = ans * d;
ans = ans + numval(s[i]);
ans = ans % q;
return ans;
The calculation for
Based on the above, the complexity for the parts outside the for-loop is
Next, for the second half of the problem, it's something you can understand with a little thought. For this algorithm
Conversely, if different strings correspond to the same hash value, the premise breaks down. As an extreme example, if you set the modulo
Therefore, as an answer to this question, something like "When hash collisions occur" would be sufficient. [5]
As long as hashes do not collide, algorithm
For example, if
What did you think? We were able to learn comprehensively about the rolling hash algorithm, its computational complexity, and the hash collision problem.
I think this problem is around the easy to standard difficulty level[6][7] among the past questions for the Computer Science major, so if you thought "This is easy!", please try applying to the Department of Computer Science, Graduate School of Information Science and Technology, at the University of Tokyo!
-
To be precise, the official name of the string search algorithm introduced here is the Rabin-Karp algorithm, and Rolling Hash refers to the hash used in the Rabin-Karp algorithm, but in this article, I will use "Rolling Hash" as a shorthand for the algorithm itself. ↩︎
-
The PDF of past questions might have been made private and become unavailable; in that case, please try searching with archival tools like archive.org. ↩︎
-
Of course, for those who already know Rolling Hash through competitive programming or other means, solving this problem on an entrance exam would have been quite easy. I passed this department this year, but I didn't know about Rolling Hash until I solved this past question... By the way, those applying to the Department of Computer Science should also master other string search algorithms like KMP and BM. ↩︎
-
To have an intuitive feel for this, I feel you might need experience in competitive programming calculating remainders divided by 1,000,000,007... ↩︎
-
In the world of competitive programming, it is said that setting the Mod to
is safe. Reference: https://qiita.com/keymoon/items/11fac5627672a6d6a9f6 ↩︎2^{61} - 1 -
Actually, there is one more small question after this, but I omitted it as it deviates slightly from the introduction to Rolling Hash. ↩︎
-
However, problems with such a strong competitive programming flavor are only part of the exam; the entrance exam as a whole covers a wide range of fields from "Information Mathematics, Numerical Analysis, Discrete Mathematics, Algorithms and Complexity, Formal Languages, Logic, Programming Language Theory, Computer Architecture, Operating Systems, Digital Circuits, and Machine Learning." Based on my view as someone who passed, you don't need to solve extremely difficult problems; you should be fine as long as you can correctly answer basic questions in any field. ↩︎
Discussion