DOMINO1 - The Longest Chain of Domino Tiles

You are given N domino tiles. Each tile is made of some number of squares (not necessarily two), and each square is coloured either white or black (we use the Croatian letters: B for white and C for black).

Find the longest chain that can be made of these tiles. Each tile can be used at most once and cannot be rotated (for example, BC cannot become CB). The chain is made by a common rule: in adjacent tiles, touching squares must be of the same colour.

Input

[N ≤ 100, the number of dominoes]

in the next N lines:

[a string of size between 1 and 100, representing the domino]

Output

The length of the longest chain.

Example

Input:
4
CB
BCC
BBCC
BCBBC

Output:
11

Added by:Adrian Satja Kurdija
Date:2011-05-01
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:own problem

hide comments
2021-12-28 17:13:32
just an implementation problem
2013-02-06 21:36:12 $$
weak test case...my code giving 4 for the case: 3 BB CC CB was accepted
2012-04-04 05:38:07 Neeraj Pradhan
ID :- 6781525 giving wrong answer. Can someone plz give some more test cases as it is giving correct answer even for the test case in the forum.
2011-10-31 18:47:19 Nitin Sharma
very nice problem !!
2011-09-14 09:33:44 Adrian Satja Kurdija
@nika: it is very useful to learn how to test your solutions. Namely, you make a brute-force solution and a (random) test-case generator, generate about 100 test cases (small enough for brute-force), find the one on which brute-force and your real solution give different answers and then debug. :)

Last edit: 2011-05-12 19:13:23
2011-09-14 09:33:44 სვანიძე
my code works on my computer but gets WA on spoj. i don't know why prog ID 5072925
2011-09-14 09:33:44 Jaros³aw Meller
Thank you very much :-) AC now but I must avoid such silly mistakes in the future...
2011-09-14 09:33:44 Jaros³aw Meller
Can someone give some tricky test cases? Trying to solve it, works fine for all mine test cases, but keep getting WA...
2011-09-14 09:33:44 Rachmawan Atmaji Perdana
Nice problem, seems like easy but ACC rate is not high. Maybe contains tricky test case

Last edit: 2011-05-05 16:51:14
2011-09-14 09:33:44 S
another way of asking sort the following :)
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.