×

# 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

This question is marked "community wiki".

3.0k93164187
accept rate: 12%

19.8k350498541

thank a lot learned the algorithm (KMP) and nice technique

(30 Jul '14, 19:05)

Why are we concatenating B with B before applying the KMP algorithm?

(14 Jun '17, 00:58)

 5 You can also use Z-algorithm. Here is how. answered 27 Jul '14, 15:00 2★Debanjan 346●5●9 accept rate: 12%
 2 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) solution link : http://www.codechef.com/viewsolution/4396688 answered 28 Jul '14, 15:09 337●4●5●14 accept rate: 0%
 2 Really Nice question. Using KMP algorithm you can easily solve it. My Solution: here 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!!! answered 30 Jul '14, 14:31 2.3k●18●30●69 accept rate: 1% Why do we need to append B to itself ? (20 Mar '16, 18:37) Why do we need to append B to itself? (14 Jun '17, 01:08) @upendra1234, Take a test case : 9 abcabdfeg abcabcabd. Your accepted code is giving answer 0 , but the correct answer is 3 . (20 Jun '17, 00:49)
 1 WHAT is the correct output FOR this input? 4 ccad ccca The ac solutions are giving different output. my solution ig giving the output 1 answered 27 Jul '14, 16:23 14●2 accept rate: 0% Mine is giving 1, which is correct. (27 Jul '14, 17:05)
 0 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 answered 27 Jul '14, 15:21 2.2k●6●17●48 accept rate: 10%
 0 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... answered 27 Jul '14, 16:13 322●9 accept rate: 23%
 0 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. answered 27 Jul '14, 16:19 180●4●6●13 accept rate: 0% try test case: 4 cccc cccc output is 0 (28 Jul '14, 09:02) Thanks a lot! That was a problem, but it still gives WA on the last one of subclass1. What can be the problem? Here's my code: http://www.codechef.com/viewsolution/4405550 (30 Jul '14, 18:21)
 0 O(N^2) with minor optimisations was also accepted: http://www.codechef.com/viewsolution/4384460 answered 27 Jul '14, 17:05 559●1●3●12 accept rate: 7%
 0 One can also solve this problem with hashes in O(N*logN). answered 27 Jul '14, 17:33 7★lebron 3.3k●3●17 accept rate: 24% How exactly? (27 Jul '14, 18:21) 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. (28 Jul '14, 11:02)
 0 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 answered 27 Jul '14, 20:12 11●1 accept rate: 0%
 0 why we need to concatenate b? answered 20 Aug '15, 19:03 25●1●1●4 accept rate: 0%
 0 answered 18 May '17, 01:23 1 accept rate: 0%
 0 refer to this CODE for above mentioned problem. answered 18 May '17, 01:24 1 accept rate: 0%
 0 Please help me with my code , I have used KMP algorithm and I am getting a wrong answer. Which might be the test cases getting wrong ?? https://www.codechef.com/viewsolution/14293528 answered 20 Jun '17, 00:59 1 accept rate: 0%
 0 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 answered 21 Aug '18, 01:01 0★ig92 1 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×2,658
×355
×76
×12

question asked: 27 Jul '14, 14:47

question was seen: 9,928 times

last updated: 21 Aug '18, 01:01