Skip to content

Assignment 5

Introductory

Problem 1 — Let's Hash

Problem statement

Consider the Rabin–Karp algorithm.


Part A

Modify the algorithm so that, after \(O(n)\) preprocessing on the text string \(S\), the hash of any substring can be computed in \(O(1)\).

Explain what information must be precomputed and stored, and how it is used to obtain a substring hash efficiently.


Part B

In the algorithm above, suppose that whenever two hashes match, we also compare the strings character by character to verify equality. How does this affect correctness, running time, and memory usage? Let the string lengths be \(m\) and \(n\).


Part C

What problem may arise if the modulus \(M\) used by the hash is very small?

Also explain why modular arithmetic is used in string hashing in the first place.


Part D

Compare string hashing with KMP and tries in terms of running time and memory usage.

Under what circumstances is string hashing preferable, and why?

hash

Problem 2 — A Trie This Time

Problem statement

Maintaining a Set of Words with a Trie

Initially, the trie is empty. A sequence of queries is then processed, and each query may change the state of the trie.

Each query is one of the following four types:

ADD word
DELETE word
SEARCH word
COUNT prefix

Words are stored as a set: if the same word is added several times, it counts only once in the trie.


Part A

Explain how to insert and delete a word in the trie.

For ADD word, insert word into the trie.

For DELETE word, remove word if it exists; otherwise leave the trie unchanged.

Also specify what information must be stored at each trie node for these operations to work correctly.


Part B

Explain how to answer the following query using the trie:

SEARCH word

This query must determine whether word is currently present in the trie.


Part C

Explain how to answer the following query using the trie:

COUNT prefix

This query must return the number of words in the trie that begin with prefix.


Part D Briefly state when a trie is preferable to the other algorithms you have learned. For example, when is using a trie more appropriate than KMP or Rabin–Karp?

trie

Problem 3 — A New Algorithm?

Problem statement

Pattern Matching with a Suffix Array

Assume a ready-made algorithm BuildSuffixArray(S) is available and returns the suffix array of string S.

That is, if we write:

SA = BuildSuffixArray(S)

then SA is an array of indices that orders the suffixes of S lexicographically.

For example, if:

S = "banana"

then:

SA = [5, 3, 1, 0, 4, 2]

because the suffixes of "banana" appear in the following lexicographic order:

5: a
3: ana
1: anana
0: banana
4: na
2: nana

In this problem, you may not explain or implement how a suffix array is built. Treat BuildSuffixArray(S) as a black box.

Using only this provided algorithm, answer the following three parts.


Part A

Given a string S and a pattern P, design an algorithm that determines whether P occurs as a substring of S.

For example:

S = "banana"
P = "ana"

the output should indicate that P occurs in S.

Hint: use the fact that suffixes are sorted lexicographically in the suffix array.


Part B

Modify the previous algorithm to return one starting position of P in S.

For example:

S = "banana"
P = "ana"

One acceptable output is:

1

because substring "ana" starts at index 1:

banana
 ana

Output 3 is also acceptable, because "ana" also starts at index 3.


Part C

Design an algorithm that finds every starting position of P in S.

For example:

S = "banana"
P = "ana"

The output should be:

[1, 3]

The order of the output does not matter.

SuffixArray

Problem 4 — KMP Tree

Problem statement

The KMP Tree

Let \(s[1..n]\) be a string of length \(n\). For each \(1 \le i \le n\), define \(lps_i\) as the length of the longest proper prefix of \(s[1..i]\) that is also its suffix. Equivalently, \(lps_i\) is the largest integer \(k \lt i\) such that:

\[s[1..k]=s[i-k+1..i]\]

The KMP tree of \(s\) is a rooted tree on vertices \(0,1,\dots,n\) in which, for every \(i \ge 1\), the parent of vertex \(i\) is \(lps_i\).

  1. For \(s=\texttt{abacaba}\), compute every \(lps_i\) and draw the corresponding KMP tree.
  2. Give a string of length \(n\) whose KMP tree has the maximum possible height.

  3. Prove that in the KMP tree of \(s\), vertex \(x\) is an ancestor of vertex \(y\) if and only if \(s[1..x]=s[y-x+1..y]\). In other words, \(x\) is an ancestor of \(y\) exactly when the length-\(x\) prefix is also a suffix of \(s[1..y]\).

  4. For each \(1 \le i \le n\), define \(sps_i\) (shortest prefix-suffix) as the length of the shortest nonempty proper prefix of \(s[1..i]\) that is also a suffix, or \(0\) if no such prefix exists. Equivalently, when it exists, \(sps_i\) is the smallest positive integer \(k < i\) such that \(s[1..k]=s[i-k+1..i]\). Design an \(O(n)\) algorithm that computes all \(sps_i\) values.

  5. The period of a string is the smallest positive integer \(k\) such that the string can be partitioned into identical blocks of length \(k\). For example, the period of \(s=\texttt{abababab}\) is \(2\). Design an \(O(n\log n)\) algorithm that computes the period of every prefix \(s[1..i]\). For example, for \(s=\texttt{ababcababc}\) the output is \([1,2,3,2,5,6,7,8,9,5]\).

kmp

Problem 5 — Trie Implementations

Problem statement

In string problems, the alphabet size is usually treated as a constant. Even so, tries are commonly implemented either with maps or with arrays.

Compare these two implementations.

Problem 6 — Twice

Problem statement

A string \(S\) is given. In \(O(|S| \log |S|)\) time, find the longest substring of \(S\) that occurs at least twice.

hash
Solution - Amirmohammad Nasrollahnejad

Problem 7 — Circular

Problem statement

Two strings \(A\) and \(B\), each of length \(n\), are given. Using hashing, give an \(O(n)\) algorithm that determines whether \(B\) is a rotation of \(A\).

hash

Problem 8 — Many Queries

Problem statement

A string \(S\) is given, followed by \(q\) queries of the following form:

\[(l_1, r_1, l_2, r_2)\]

For each query, determine whether the following two substrings are equal:

\[S[l_1 \dots r_1]\]
\[S[l_2 \dots r_2]\]

Give the best bounds you can achieve for preprocessing and for answering each query.

hash
Solution - Noushin Javadzadeh

Problem 9 — Palindrome Queries

Problem statement

A string \(S\) of length \(n\) and \(q\) queries are given. Each query specifies an interval \([l,r]\); determine whether \(S[l \dots r]\) is a palindrome. Give an \(O(n+q)\) algorithm.

hash

Problem 10 — The Engineer and His String

Problem statement

A string \(S\) of length \(n\) and a token that may initially be placed on any character of the string are given.

After placing the token, we may move it right any number of times, possibly zero. A move from position \(i\) takes it to position \(i+1\); it cannot move right from the last position.

After completing the rightward moves, we may move the token left any number of times, possibly zero. A move from position \(i\) takes it to position \(i-1\); it cannot move left from the first position.

Whenever we place or move the token, we write down the character on which it lands.

For example, if \(S\) is abcdef, we place the token on the third character, move it right twice, and then left three times, the recorded string is cdedcb.

Given strings \(S\) and \(T\), determine in \(O(n^2)\) time whether the described process can be performed on \(S\) so that the recorded string is exactly \(T\).

hash
Solution - Mohsen Zare
Attachments

Problem 11 — Shengduobao's String Game

Problem statement

In typical cases, Boyer–Moore may use roughly \(O(N/M)\) comparisons, but in the worst case it can require about \(O(MN)\) comparisons.

To demonstrate this, Shengduobao gives you the following strings:

\(T = \texttt{BBBBBBBBBB}\)

\(P = \texttt{ABBBB}\)


Run the Boyer–Moore algorithm with the mismatched-character heuristic on this input.


Report the shift, or skip distance, used at each step.


He also asks for the total number of character comparisons. Help him compute it.


Impressed by your expertise, Shengduobao asks why this input is bad for Boyer–Moore.


Finally, explain why Boyer–Moore is usually fast in practice despite this worst case.

Solution - Ayeh Saberi
Attachments

Problem 12 — Boyer–Moore Shifts

Problem statement

A text \(T\) of length \(n\) and a pattern \(P\) of length \(m\) are given. We want to find every position where \(P\) occurs in \(T\).

Explain the Boyer–Moore algorithm for this problem. State why it compares characters from the end of the pattern and how a mismatch can be used to shift the pattern forward by several positions.

Problem 13 — KMP

Problem statement

A string \(S\) of length \(n\) is given. For every \(i\), let \(\pi_i\) be the length of the longest proper prefix of \(S[1..i]\) that is also a suffix of \(S[1..i]\).

Give an \(O(n)\) algorithm that computes every \(\pi_i\).

Problem 14 — Pattern Matching

Problem statement

Strings \(S\) and \(P\) are given. Find every position where \(P\) occurs in \(S\).

Overlapping occurrences count separately. For example, if \(S=abababa\) and \(P=aba\), the answer is positions \(1\), \(3\), and \(5\).

Give an \(O(|S|+|P|)\) algorithm.

Problem 15 — Borders

Problem statement

A string \(S\) is given. A string \(X\) is a border of \(S\) if it is both a proper prefix and a suffix of \(S\).

For example, in \(ababcabab\), the strings \(ab\) and \(abab\) are borders.

Give an \(O(n)\) algorithm that finds the lengths of all borders of \(S\).

Problem 16 — Number of Borders of Each Prefix

Problem statement

A string \(S\) is given. For every prefix \(S[1..i]\), compute its number of proper borders.

For example, if \(S=ababab\), then the prefix \(S[1..6]=ababab\) has two borders, of lengths \(2\) and \(4\).

Give an \(O(n)\) algorithm.

Problem 17 — Counting Prefix Occurrences

Problem statement

A string \(S\) is given. For every prefix of \(S\), compute how many times it occurs in the entire string \(S\).

For example, in \(ababab\), the prefix \(ab\) occurs three times.

Give an \(O(n)\) algorithm.

Solution - Matin Ghiasi
Attachments

Problem 18 — Short Palindrome

Problem statement

A string \(S\) is given. We want to make it a palindrome by prepending characters.

Find the minimum number of characters required, or construct the shortest resulting string.

For example, for \(S=abcd\), one answer is \(dcbabcd\).

Give an \(O(n)\) algorithm.

Problem 19 — Repeated String

Problem statement

A string \(S\) is given. Determine whether \(S\) consists of several repetitions of a shorter string.

For example, \(ababab\) is formed by repeating \(ab\), whereas \(ababa\) is not periodic in this way.

Give an \(O(n)\) algorithm.

Problem 20 — Valid Shifts

Problem statement

A string \(S\) is given. Call an integer \(p\) valid if \(S_i=S_{i+p}\) for every position \(i\) for which both \(i\) and \(i+p\) lie in the string.

Find all valid values of \(p\).

Give an \(O(n)\) algorithm.

Solution - Fatemeh Parvizi
Attachments

Problem 21 — Cyclic Shift

Problem statement

Two strings \(A\) and \(B\) of equal length are given. Determine whether a cyclic shift of \(A\) can produce \(B\).

For example, if \(A=abcde\) and \(B=cdeab\), the answer is yes.

Give an \(O(n)\) algorithm using KMP.

Problem 22 — Multiple Copies, Minimum Length

Problem statement

A string \(P\) and an integer \(k\) are given. Construct the shortest string containing \(k\) copies of \(P\).

The copies may overlap. For example, if \(P=aba\) and \(k=3\), then \(abababa\) contains three copies of \(P\).

Give an \(O(|P|+k)\) algorithm.

Solution - Sana Niromand
Attachments

Problem 23 — Shortest Superstring of Two Strings

Problem statement

Two strings \(A\) and \(B\) are given. Find a shortest string containing both \(A\) and \(B\) as substrings.

If one string already occurs inside the other, the longer string may be the answer. If several answers have the same length, any one is acceptable.

Give an \(O(|A|+|B|)\) algorithm.

Problem 24 — Camp Schedule

Problem statement

A multiset of 0 and 1 characters and a target binary string \(T\) are given.

Use the available characters to construct a string that maximizes the number of occurrences of \(T\).

Give an algorithm that constructs such a string.

Problem 25 — The KMP Automaton

Problem statement

A pattern \(P\) and an alphabet \(\Sigma\) are given. For every state from \(0\) through \(|P|\) and every character \(c \in \Sigma\), determine the state reached after reading \(c\).

Give an \(O(|P| \cdot |\Sigma|)\) algorithm.

Problem 26 — Repeated Block

Problem statement

A pattern \(P\), a string \(B\), and a large integer \(k\) are given. Let \(B^k\) denote \(k\) consecutive copies of \(B\).

Count the occurrences of \(P\) in \(B^k\) without constructing the whole string \(B^k\).

Give an algorithm that remains efficient for large \(k\).

Solution - Mohammad Esmaeili Marandi

Problem 27 — Alice

Problem statement

Strings \(G_1,G_2,\dots,G_n\) are defined. Each \(G_i\) is either an ordinary string \(S_i\) given directly, or the concatenation of two earlier strings:

\(G_i = G_j \cdot G_k\)

A pattern \(P\) and an index \(q\) are given. Count the occurrences of \(P\) in \(G_q\) without constructing all of \(G_q\).

Give an algorithm that avoids building extremely long strings.

Problem 28 — Bob

Problem statement

Two patterns \(A\) and \(B\) are given. Find the shortest string that contains \(A\) but does not contain \(B\).

Report if no such string exists.

Give an efficient algorithm.

Problem 29 — Charlie

Problem statement

A pattern \(P\), an integer \(L\), and a target state \(q\) of the KMP automaton are given.

Find the lexicographically smallest string of length \(L\) that leaves the automaton for \(P\) in state \(q\) after processing it.

Report if no such string exists.

Advanced

Problem 30 — BigPetr

Problem statement

Strings \(W\) and \(S\) are given. Consider all substrings of \(S\) of length \(|W|\), retaining only those that are permutations of \(W\): they contain exactly the same characters with the same multiplicities, though possibly in a different order.

Among these valid substrings, find one with the greatest number of occurrences in \(S\). If several valid substrings attain the maximum, choose the lexicographically smallest one.

If \(S\) contains no valid substring, report that no answer exists.

hash

Problem 31 — Petr

Problem statement

A string \(t\) and two strings \(s_{begin}\) and \(s_{end}\) are given. In \(O(N^2\log N)\) time, count the distinct substrings of \(t\) that begin with \(s_{begin}\) and end with \(s_{end}\).

Two substrings are considered distinct when their contents differ. Thus, an identical substring occurring at several positions of \(t\) is counted only once.

Note that \(s_{begin}\) and \(s_{end}\) may be equal or may overlap within a substring.

hash
Solution - Sobhan Aram

Problem 32 — Two-Dimensional String

Problem statement

A large matrix of characters and a smaller two-dimensional pattern are given. Find every position where the pattern occurs exactly in the matrix, with the best running time you can achieve.

hash
Solution - Rasa Mohammadi

Problem 33 — Twins

Problem statement

A social network contains \(n\) profiles numbered from \(1\) to \(n\). Some pairs of profiles are friends, and friendship is mutual.

Two distinct profiles \(i\) and \(j\) are called twins if, for every other profile \(k\) with \(k \ne i\) and \(k \ne j\), one of the following holds:

Profile \(k\) is friends with both \(i\) and \(j\). Profile \(k\) is friends with neither \(i\) nor \(j\).

Profiles \(i\) and \(j\) themselves may or may not be friends; this does not affect whether they are twins.

In \(O((N+M)\log N)\) time, count the unordered pairs \((i,j)\) whose profiles are twins. Since the pairs are unordered, \((i,j)\) and \((j,i)\) are identical.

hash
Solution - Sepehr Alipour

Problem 34 — Martians

Problem statement

While studying Martians, Petya learned that they are extremely lazy: they love sleeping and hate waking up.

A Martian has exactly \(n\) eyes arranged in a row and numbered from \(1\) to \(n\) from left to right. Before sleeping, the Martian covers every eye with an eye patch so that the morning light does not wake him. An uppercase Latin letter is written on the inside of each patch. Thus, after waking and opening all his eyes, he sees an uppercase string \(s\) of length \(n\).

The alarm rings and the Martian wakes, but he has not opened any eyes yet. Sensing a difficult day ahead, he wants to open his eyes and see something beautiful.

The Martian considers only \(m\) Martian words beautiful. Opening every eye at once is difficult so early in the morning, so he opens two disjoint, non-overlapping segments of consecutive eyes.

More precisely, he chooses four integers \(a\), \(b\), \(c\), and \(d\) such that:

\(1 \le a \le b < c \le d \le n\)

He then opens every eye whose index \(i\) satisfies one of the following conditions:

\(a \le i \le b\)

or

\(c \le i \le d\)

After opening those eyes, he reads all visible characters from left to right and thereby sees a word.

Consider all distinct words the Martian can see in the morning. Determine how many of them are beautiful.

Problem 35 — Bears and Elephants

Problem statement

Polar bears Menshykov and Uslada from the Saint Petersburg Zoo and Horace the elephant from the Kyiv Zoo found many wooden blocks. They began stacking the blocks into towers. Several towers standing side by side in a row form a wall, whose towers may have different heights.

Horace finished first and named his wall Elephant. His wall consists of \(w\) towers. The bears also built an unnamed wall consisting of \(n\) towers.

Horace now looks at the bears' wall and wants to know in how many segments he can see his Elephant.

He sees an elephant in a segment of \(w\) consecutive towers if the sequence of tower heights in that segment matches the height sequence of Horace's wall.

To see more elephants, Horace may move his wall vertically, even below ground level. Therefore, the heights need not be exactly equal: the overall shape only needs to match after adding a fixed vertical offset to every height.

Give an \(O(n+w)\) algorithm that counts the segments of the bears' wall in which Horace can see his Elephant.

Problem 36 — Same Shape

Problem statement

Two numeric arrays \(A\) and \(B\) are given. Find every position where \(B\) occurs in \(A\) with the same shape: adding the same constant to every element of \(B\) still counts as a match.

For example, if \(B=[3,5,8]\), then \([10,12,15]\) has the same shape.

Give an \(O(n+m)\) algorithm.

Solution - Mohammad Mahmoudieh

Problem 37 — Splitting the Pattern

Problem statement

Two strings \(S\) and \(T\) are given. Determine whether \(T\) can be split into two parts \(A\) and \(B\), with \(T=AB\), such that \(S\) has the following form:

\(*A*B*\)

That is, \(A\) must occur somewhere in \(S\), and \(B\) must occur later in \(S\); anything may lie between them.

Give an efficient algorithm.

Solution - Mahdiar Mostashar

Problem 38 — David

Problem statement

A forbidden pattern \(P\), an integer \(n\), and an alphabet \(\Sigma\) are given.

Count the strings of length \(n\) over \(\Sigma\) that do not contain \(P\).

Give an algorithm using the KMP automaton.

Solution - Seyedeh Shaghayegh Mirjalili

Problem 39 — Eve

Problem statement

A string \(S\) containing some ? characters and a pattern \(P\) are given.

Each ? may be replaced by any alphabet character. Find a replacement that maximizes the number of occurrences of \(P\) in the resulting string.

Give an algorithm.

Solution - Shayan Sabzi

Problem 40 — LCS Without a Forbidden Pattern

Problem statement

Two strings \(A\) and \(B\) and a forbidden pattern \(P\) are given.

Find a longest common subsequence of \(A\) and \(B\) that does not contain \(P\) as a substring.

Give an algorithm.

Solution - Sobhan Behzadipour