An intuitive alternative solution to a hard problem

Revision en1, by flaviu2001, 2018-08-29 02:12:00

I recently came across a beautiful problem from the Romanian national olympiad from 2005. It has a beautiful intended solution but i will provide another you might have tried if you couldn't find it.

A sequence is called circular if its elements consist of only 'A' and 'B', it has size n (n >= 1) and the element next to the last is considered to be the first. A consequence of such a sequence is that there are precisely n contiguous subsequences (from now on whenever we mention subsequence we mean a contigous one) of any size, for example for "ABBA" the 4 subsequences of size 3 are in order "ABB", "BBA", "BAA", "AAB".

A sequence is called special if it satisfies the property of the circular sequence defined above and furthermore ANY two subsequences of equal size differ in the number of characters 'A' by at most 1. For example "AABB" is not special because there exist "AA" and "BB", "ABABAABAAB" is not special either because of "AABAA" and "BABAB", but "ABA" and "AABABAAB" satisfy the property.

Your task is given a circular sequence to determine whether it satisfies the special property. Consider as examples the 4 sequences given above.

A first step towards a solution is to observe that the sequence must consist of concatenations of "nAB" and "n+1AB" where "nAB" means n chars of 'A' and one 'B' (or "nBA" but we will consider just "nAB" from now on). We can consider a second sequence where every occurence of "nAB" in the original seq. is marked with 'A' and "n+1AB" is considered 'B'. For example "AABAAABAABAABAAAB" becomes "ABAAB". The key observation (and likely difficult to spot) is that the initial sequence is special if and only if the second one is special, thus we can simplify the sequence until we reach a trivial case. This approach takes O(n) steps.

But suppose we didn't figure that. Intuitively we notice that the 'B's are evenly distributed across the subsequence, meaning the spaces between consecutive 'B's must not differ by much. Thus we can actually 'measure' how much the 'B's differ from their supposed positions, let me show you what i mean on an example, take ABAABA. By the way, we can simplify the problem by doubling the sequence and thus becomes ABAABAABAABA and only take subsequences of size n (here n=6) into consideration.

Tags olympiad, romania

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English flaviu2001 2019-04-27 15:10:54 7 Tiny change: 'n above.\n\n\nA fi' -> 'n above.\n[cut]\n\n\nA fi'
en2 English flaviu2001 2018-08-29 02:43:28 1953 (published)
en1 English flaviu2001 2018-08-29 02:12:00 2348 Initial revision (saved to drafts)