ABCD - Colours A, B, C, D

no tags 

Consider a table with 2 rows and 2N columns (a total of 4N cells). Each cell of the first row is coloured by one of the colours A, B, C, D such that there are no two adjacent cells of the same colour. You have to colour the second row using colours A, B, C, D such that:

  • There are exactly N cells of each colour (A, B, C and D) in the table.
  • There are no two adjacent cells of the same colour. (Adjacent cells share a vertical or a horizontal side.)

It is guaranteed that the solution, not necessarily unique, will always exist.


[a natural number N ≤ 50000]

[a string of 2N letters from the set {A, B, C, D}, representing the first row of the table]


[a string of 2N letters from the set {A, B, C, D}, representing the second row of the table]





hide comments
nadstratosfer: 2018-01-27 16:56:15

Got best time in Py3 with the same O(n) code that TLEd until I made a certain assumption about test cases. Also, with 18 separate testfiles my runtime of 0.37s mostly measures the total of interpreter startups (same code ACs in 0.08s in Py2 where interpreter launches faster). Pointless trying to optimize when AC itself is a lottery. Frustratingly stupid way of setting an otherwise interesting problem.

Last edit: 2018-01-27 17:38:19
mark42: 2018-01-27 09:49:44

AC in one go.. seriously this piece of spoj made my day

biswajitk123: 2018-01-12 22:32:40

awesome logic!simply beautiful!!!

d_y1997: 2017-12-22 13:19:18

18th testcase if wrong ..
possible ans: CDACAB

ayushgupta1997: 2017-12-21 19:53:32

Became one of my fav problem :) .Got one runtime error as max size of array will be 2*n I took n. Solved myself, made my day!!
Real test case are after 16....I think!

dunjen_master: 2017-07-24 23:50:31

stuck on test case 18....ques is very easy though i am wondering where am i making mistake

candide: 2017-04-11 12:41:30

Accurate problem description. Solution needs much more tricky logic than algorithmics. Keep it simple! remember: you have 4 colors and each row has even length ...

Pratik Ritesh: 2017-03-21 15:21:48

really nice problem..try breaking down to subproblem (keep in mind length of given row is even)..:)

stark_attack: 2017-02-17 08:23:35

getting WA answer after 18th testcase any suggestions .

vivek4434: 2017-01-30 21:10:37

what is meant by first condition ??

Added by:Adrian Satja Kurdija
Time limit:0.190s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU
Resource:inspired by a math puzzle