Answers to: ANKPAL - Editorialhttps://discuss.codechef.com/questions/71400/ankpal-editorial<h1>PROBLEM LINK:</h1>
<p><a href="http://www.codechef.com/SNCK15EL/problems/ANKPAL">Contest</a><br>
<a href="http://www.codechef.com/problems/ANKPAL">Practice</a> </p>
<p><strong>Author:</strong> <a href="http://www.codechef.com/users/code_master01">Ankit Srivastava</a><br>
<strong>Tester:</strong> <a href="http://www.codechef.com/users/kevinsogo">Kevin Atienza</a> and <a href="http://www.codechef.com/users/stzgd">Gedi Zheng</a><br>
<strong>Editorialist:</strong> <a href="http://www.codechef.com/users/kevinsogo">Kevin Atienza</a> </p>
<h1>PREREQUISITES:</h1>
<p>String processing, string polynomial hashing</p>
<h1>PROBLEM:</h1>
<p>You are given a string $S$, and you have to answer $Q$ queries. Each query consists of four integers $(i,j,k,l)$, and you need to tell whether the substring from the $k$th position to the $l$th position is a palindrome assuming the substring from the $i$th position to the $j$th position is reversed <em>temporarily</em> (i.e. reversals are only applied per query and do not persist for further queries).</p>
<h1>QUICK EXPLANATION:</h1>
<p>Fix a (prime) modulus $M$ (preferably between $10^9$ and $2\cdot 10^9$), and a base $B$ (preferably a generator modulo $M$) and define the <strong>polynomial hash</strong> of the string $S = S_0S_1\cdots S_{n-1}$ as the following:
$$\text{hash}(S) = \left(\sum_{i=0}^{n-1} H[S_i]B^i\right) \bmod M$$
where $H[c]$ is some fixed value from $0$ to $M-1$ assigned to $c$.</p>
<p>If $M$ and $B$ are chosen well, two strings $S$ and $T$ are equal if and only if $\text{hash}(S) = \text{hash}(T)$ with high probability, and $S$ is a palindrome iff $\text{hash}(S) = \text{hash}(S^R)$ with high probability ($S^R$ is the reversal of $S$).</p>
<p>If we are given two strings $S$, $T$ and their hashes, then the hash of $ST$ (their concatenation) can easily be computed in $O(1)$ time (assuming we have precomputed the powers of $B$).</p>
<p>Preprocess $S$ so that we can compute the polynomial hash of any substring and reversal of any substring in $O(1)$ time. </p>
<p>Next, to answer the query $(i,j,k,l)$, first check if $[k,l]$ intersects with $[i,j]$. If not, we can simply compute the hash of $S[k,l]$ and its reverse. Otherwise, split $[k,l]$ into three parts, where the middle part is contained completely in $[i,j]$. The hashes of the three parts (and their reverses) can be computed separately (with the middle one requiring special care), and can be concatenated in $O(1)$ time and compared. If the hash of the string and its reverse match, then the answer is "Yes", otherwise it is "No".</p>
<h1>EXPLANATION:</h1>
<p>When faced with a problem, there are a couple of helpful general tips and strategies that might help you solve it:</p>
<ul>
<li>Solve a simpler version of the problem</li>
<li>Use one of the standard algorithms for this type of problem</li>
</ul>
<p>There are times when these tips do not apply, and those cases usually require a lot more thinking and thus are usually more interesting. But let's try to apply these strategies in the problem at hand.</p>
<h1>Solve a simpler version</h1>
<p>How can we simplify the problem? Well, of course we can reduce the bounds of $|S|$ and $Q$ to, say, $\le 10^3$, but in that case the answer is easily brute-forceable, so it doesn't get us closer to a fast solution.</p>
<p>Well, we can simplify the problem by removing the "reversal" operation for each query, i.e. each query consists of two integers $(k,l)$, and you need to tell whether the substring from the $k$th position to the $l$th position is a palindrome. (You can view this simplified problem as a special case of the original problem with $i = j = 1$, for example) Now this simplification is more interesting :)</p>
<p>In this case, one can apply standard palindrome algorithms like <a href="https://en.wikipedia.org/wiki/Longest_palindromic_substring">Manacher's algorithm</a> to determine the longest palindrome centered at each position. This takes $O(|S|)$ preprocessing and $O(1)$ query time. However, this solution doesn't adapt well once we add in the reversal operations, so let's try to find another solution.</p>
<h1>Use a standard algorithm</h1>
<p>At this point, we try to find what else can be done to check for palindromes. Well, what else are standard algorithms for this task? Hashing, of course! In fact, in many occasions one might prefer a hashing algorithm instead of Manacher's algorithm because it's more flexible, and easier to implement (whereas Manacher's algorithm can be tricky to implement, and is not as flexible).</p>
<p>A string is palindromic if and only if it is equal to its reversal. However, the reversal of any substring of a string $U$ is a specific substring of the reversal of $U$. In more detail, if we denote the reversal of a string $S$ by $S^R$, then we're saying that if $U[i..j]$ is a substring, then $U[i..j]^R = U^R[N+1-j..N+1-i]$, where $N$ is the length of $U$. Therefore, we have reduced a query into a case of comparing whether substrings of two strings are equal. But substring comparisons can be done quickly using <em>polynomial hashing</em>!</p>
<h1>Polynomial Hashing</h1>
<p>A <strong>hash</strong> is simply a function that maps an object of an arbitrary size (such as a string) into an value of fixed size (such as a bounded integer).</p>
<p>There will inevitably be pairs of objects that will be mapped to the same value (in fact, infinitely many of them). We call a situation where two different objects are mapped to the same hash value a <strong>collision</strong>. Collisions are inevitable, however we desire hash functions such that it is "random" enough so that for any string we will hash, it will be assigned a hash value with equal "probability" among all possible hash values, and that it is not particularly easy to generate different strings with the same hash value. </p>
<p>One application of hash values is in comparing strings: If our hash function is good enough, then two different strings have hashes with high "probability". Let's be a bit more specific. Suppose that our hash function maps strings to integers from $0$ to $M-1$. Suppose that we have two different strings $(S,T)$. If we assume that the hash function is "random" enough, then the "probability" that $S$ and $T$ have different hashes is $\approx 1 - 1/M$, which is pretty close to $1$ assuming $M$ is large. If we want to compare $K$ pairs of different strings $(S_1,T_1), \ldots (S_K,T_K)$, then the probability that every pair have different hashes is $\approx (1 - 1/M)^K$.</p>
<p>In our case, we will need to compare $K \approx 10^5$ pairs of strings, so if we choose $M$ to be around $10^9$, then the probability that no collision will occur is approximately $(1 - 1/10^9)^{10^5} \approx .9999$. Therefore, our chances of succeeding are pretty high!</p>
<p>One common hash function is the <strong>polynomial hash</strong>. To compute polynomial hashes, we first fix two integers $M$ and $B$. Also, for every character $c$ alphabet, we assign a fixed value $H[c]$ (for example, the ASCII value of $c$). Then the polynomial hash of the string $S = S_0S_1\cdots S_{n-1}$ is the following:
$$\text{hash}(S) = \left(\sum_{i=0}^{n-1} H[S_i]B^i\right) \bmod M$$
Clearly the result is a number in $[0,M-1)$, but to minimize potential easy "collisions", we ensure that $M$ is prime, and that the order of $B$ modulo $M$ is reasonably large. </p>
<p>Why did we choose the polynomial hash? Because it has other advantages that will be useful in our case:</p>
<ul>
<li>Given any two strings $S$ and $T$, the hash of the concatenation $ST$ can be quickly computed if we know the hashes of $S$ and $T$, because of the following identity
$$\text{hash}(ST) = \left(\text{hash}(S) + \text{hash}(T)B^{|S|}\right) \bmod M$$</li>
<li>Given any two strings $S$ and $T$, the hash of $T$ can be quickly computed if we know the hashes of $S$ and $ST$, because of the following identity (which follows easily from the previous identity)
$$\text{hash}(T) = \left(\text{hash}(ST) - \text{hash}(S)\right)B^{-|S|} \bmod M$$</li>
<li>Given any string $S$, one can easily preprocess it so that the hash of any substring $S[i..j]$ can be computed in $O(1)$ time. To do so, we first precompute the hashes of <em>all prefixes</em> of $S$. Let's denote the hash of $S[1..j]$ by $h(j)$. This can be done in $O(|S|)$ time using the first identity:
$$h(i) = \left(h(i-1) + H[S_i]B^{i-1} \right) \bmod M$$
Now, with the array $h$ (and precomputed powers of $B$ and $B^{-1}$ modulo $M$), the hash of the substring $S[i..j]$ can now be computed using the second identity:
$$\text{hash}(S[i..j]) = \left(h(j) - h(i-1)\right)B^{-(i-1)} \bmod M$$</li>
</ul>
<p>This is great, because this gives us an $O(1)$-time algorithm to answer our query: preprocess the string $S$ and its reversal $S^R$ as above, and simply check if the hashes of $S[k..l]$ and $S[k..l]^R = S^R[N+1-l..N+1-k]$ are equal! The overall algorithm runs in $O(|S| + Q)$.</p>
<h1>Solving the problem with reversals</h1>
<p>Now, we go back to the original problem, where each query has a reversal operation. Let's try to answer the query $(i,j,k,l)$, where the substring $S[i..j]$ is reversed <em>temporarily</em>.</p>
<p>Our goal is to be able to compute hashes of substrings assuming that $S[i..j]$ has been reversed. Let's handle the simple cases first:</p>
<ul>
<li>If $[k,l]$ doesn't overlap with $[i,j]$, then we can just ignore the reversal operation, and the hash of $S[k..l]$ is still the same.</li>
<li>Next, if $[k,l]$ is completely contained in $[i,j]$, then a simple adjustment can be done: The hash of $S[k..l]$ in the updated string is just the hash of $S^R[N+1-i-j+k..N+1-i-j+l]$ :D (why?)</li>
</ul>
<p>Therefore, we only have two remaining cases to handle:</p>
<ul>
<li>$[i,j]$ partially overlaps with $[k,l]$</li>
<li>$[i,j]$ is completely contained in $[k,l]$</li>
</ul>
<p>But in these cases, we can split the string $S[k..l]$ into at most three parts, where each part is either completely contained in $S[i..j]$, or completely outside. We have just described above how compute the hashes of each of the parts quickly, and we can compute the hash of the concatenation of two strings quickly (using the identity in the previous part). Therefore, we now have an algorithm to compute the hash of any substring $S[k..l]$ assuming some substring $S[i..j]$ is reversed temporarily! Since we only compute hashes of a fixed, bounded number of parts, this operation takes $O(1)$.</p>
<p>Since we can now compute hashes of substrings even when some substring is temporarily reversed, we can now answer queries $(i,j,k,l)$ in $O(1)$ time!</p>
<h1>Further readings</h1>
<p>Here are some other links about polynomial hashing that might be useful to you:</p>
<ul>
<li><a href="http://www.mii.lt/olympiads_in_informatics/pdf/INFOL119.pdf">http://www.mii.lt/olympiads_in_informatics/pdf/INFOL119.pdf</a></li>
<li><a href="http://codeforces.com/blog/entry/4898">http://codeforces.com/blog/entry/4898</a></li>
</ul>
<h1>Time Complexity:</h1>
<p>$O(|S| + Q)$ </p>
<h1>AUTHOR'S AND TESTER'S SOLUTIONS:</h1>
<p><a href="http://www.codechef.com/downloads/Solutions/SNCK15EL/Setter/ANKPAL.cpp">Setter</a><br>
<a href="http://www.codechef.com/downloads/Solutions/SNCK15EL/Tester1/ANKPAL.cpp">Tester 1</a><br>
<a href="http://www.codechef.com/downloads/Solutions/SNCK15EL/Tester2/ANKPAL.cpp">Tester 2</a> </p>enSun, 28 Oct 2018 08:50:59 +0530Answer by arpit728https://discuss.codechef.com/questions/71400/ankpal-editorial/139021<p><a href="/users/17741/kevinsogo">@kevinsogo</a></p>
<p>Can you please help me understand how hashing is going to work here since their are chances of collision. Would't there be any test case with collision, how this will be handled.</p>
<p>Also point me to some good sources to learn implementing hashing, I am not yet clear about choosing B and M.</p>arpit728Sun, 28 Oct 2018 08:50:59 +0530https://discuss.codechef.com/questions/71400/ankpal-editorial/139021Answer by shivan111https://discuss.codechef.com/questions/71400/ankpal-editorial/122465<p>Can you please do something about the broken solution links <a href="/users/1/admin">@admin</a>. Also, does anyone know what M and B values we should use ?</p>shivan111Sat, 03 Feb 2018 20:06:23 +0530https://discuss.codechef.com/questions/71400/ankpal-editorial/122465Answer by eight_bitguyhttps://discuss.codechef.com/questions/71400/ankpal-editorial/101775<p>Still can't see the Setter's and Tester's Code , <a href="https://discuss.codechef.com/users/1/admin"><a href="/users/1/admin">@admin</a></a> do look into the matter.</p>eight_bitguyWed, 14 Jun 2017 14:14:26 +0530https://discuss.codechef.com/questions/71400/ankpal-editorial/101775Answer by Ankit Aggarwalhttps://discuss.codechef.com/questions/71400/ankpal-editorial/72103<p>Can you please explain this point
"Next, if [k,l] is completely contained in [i,j], then a simple adjustment can be done: The hash of S[k..l] in the updated string is just the hash of SR[i+j−l..i+j−k]"
??</p>Ankit AggarwalMon, 22 Jun 2015 20:17:15 +0530https://discuss.codechef.com/questions/71400/ankpal-editorial/72103Answer by harrypotter192https://discuss.codechef.com/questions/71400/ankpal-editorial/71451<p>The solution links seem to be broken. </p>harrypotter192Sat, 13 Jun 2015 10:51:06 +0530https://discuss.codechef.com/questions/71400/ankpal-editorial/71451Answer by mangalam1https://discuss.codechef.com/questions/71400/ankpal-editorial/71444<p>I thought of a different way, but was not able to implement it in time.
What I thought was to do kind of Countsort on the string S and instead of frequencies ..remember the positions of occurrence in a 2-D array. Done in O(S) time.</p>
<p>Later for each query, we map the given reversed k to l index to original string's i to j in constant time.Then find out the mid and use binary search on each of the 26 columns of the 2-D matrix to find out if the occuring places are mirrored around the fixed middle point in O( Qlog(n)) time at worst.</p>
<p>Leading to O(S+Qlog(S)) time complexity .. although little weaker but was easier to think for me..:)</p>mangalam1Sat, 13 Jun 2015 03:58:25 +0530https://discuss.codechef.com/questions/71400/ankpal-editorial/71444