# TASHIFT - Editorial

Difficulty : Simple

Pre-requisites : Strings, KMP

Problem : Given two String A and B of the same length. You can do some (may be none) shift operations which results in the first character of B moving to the end. What is the minimum number of operations that are needed so that B has the longest common prefix with A.

#### Explanation

At first, let’s consider some partial solutions.

### How to get 30 points

Just try implement the shift operations in O(N) and try all possible numbers of Shift operations on B. You properly even do not need write so much code if your language have some built-in library for string. You also do not have to do anything to speed up the string comparison. The overall complexity is O(N2).

### How to get 60 points

Let’s say we append string B at the end of string B. We get: B1,B2…BNB1,B2…BN. The thing worth noting is that after K shift operations we get BK+1,BK+2…BN,B1,B2…BK. This string(which we get after K shift operations) is now a substring of the new string that we got by appending B at the end of B.
So we’ll now search all prefixes of A in B.B(means concatanation of B with B) which will help us in knowing which prefixes of A are present in B.B.
Let’s say we have to search a string A of length M in a string B of length N. What we can do is generate hash of all substrings of B which are of length M and search for hash of string A in the generated hashes.
A hash function of string S could be say Summation[i=0 to length][26i*(S[i]-‘a’)] modulo some prime P. If we have the hash of a substring of length N say S[i,i+N], we can easily generate hash of S[i+1,i+N+1] by using inverse modulo since we are using a prime for modulo operations.

### How to get 100 points

You need to know string matching algorithm KMP to get full points. You can learn KMP here, here, or anywhere you like to :).
KMP helps us in finding all prefixes of a string X in string Y. So we’ll search A in B.B(means concatanation of B with B) which will help us in knowing which prefixes of A are present in B.B. We’ll choose an answer that gives us the largest prefix, also it’ll give the index at which that prefix matches, from which we can find the number of shifts required.

Solutions : setter tester

14 Likes

You can also use Z-algorithm. Here is how.

5 Likes

Can any one please say why my code fails(I know my code gives TLE but it shows WA)

http://www.codechef.com/viewsolution/4391697

http://www.codechef.com/viewsolution/4391999

My solution gives TLE in one-task of subtask 2 and 3. Can someone check it and tell why it is so…

can somebody please tell me on which test case is it failing? My code : http://ideone.com/HKkgUZ
It is failing on the last testcase of subclass1.

WHAT is the correct output FOR this input?
4
ccca

The ac solutions are giving different output.
my solution ig giving the output 1

1 Like

O(N^2) with minor optimisations was also accepted: http://www.codechef.com/viewsolution/4384460

One can also solve this problem with hashes in O(N*logN).

Can someone check my solution, i used a simple suffix array (created by using manber’s)for the question and i was very much sure that the approach can do this task in O(n * log^2(n)) but it resulted in TLE even when n is 10^4.
Here is the code of my solution.
http://www.codechef.com/viewsolution/4391961

Even the test cases of this problem are weak … check out my submission ,it passes without appending string B… (i.e; it passes directly by finding prefix of A in B)

2 Likes

Really Nice question. Using KMP algorithm you can easily solve it.

My Solution: [here][1]

Algorithm:

1. B = B + B; // append string B to B itself.

2. Generate lps[] array for string A.

3. Now the main algorithm. start traversing the second string and keep track of the maximum prefix found.

int res = 0, best = 0, ans = 0;
int j = 0;
i = 0;
while(i < n) {
if(A[j] == B[i]) {
j++;
i++;
best++;
}
if(best > res) {
res = best;
ans = i-j;
}
if(j == m) {
break;
}
if(A[j] != B[i]) {
best = 0;
if(j != 0) {
j = lps[j-1];
}else {
i++;
}
}
}
printf("%d\n", ans);

Ask me if anything is not clear.

Happy Coding!!!
[1]: http://www.codechef.com/viewsolution/4404669

2 Likes

why we need to concatenate b?

refer to this

``````
[1] for above mentioned problem.

[1]: https://www.codechef.com/viewsolution/13622248``````

Which might be the test cases getting wrong ??
https://www.codechef.com/viewsolution/14293528

could someone give me a counterexample for my code. i’m getting WA but cannot find a simple counterexample.
code: https://www.codechef.com/viewsolution/19812182

Mine is giving 1, which is correct.

How exactly?

try test case:
4
cccc
cccc

output is 0

One way I thought of in the contest was this: First you double B. For both A and B you store hashes in order to be easy to find the hash value of a substring( eg hash(A[2…5]) ). After that, for every suffix of B you search( using binary search) the longest common prefix with A. You will need hashes to compare in O(1) strings.