Break The Friendship Band

0

0 votes
Problem

On the divine friendship day, "A" tied "B" a friendship band. But alas! Frequent quarrels started between them and "B" decided to remove and break off the band! The band isnt a huge one and consists of red, blue and white beads in random positioning. Here are two examples for number of beads = 29:

            1 2                               1 2
        r b b r                           b r r b
      r         b                       b         b
     r           r                     b           r
    r             r                   w             r
   b               r                 w               w
  b                 b               r                 r
  b                 b               b                 b
  b                 b               r                 b
   r               r                 b               r
    b             r                   r             r
     b           r                     r           r
       r       r                         r       b
         r b r                             r r w
        Figure A                         Figure B
                    r red bead
                    b blue bead
                    w white bead

The beads considered first and second in the text that follows have been marked in the picture.

The configuration in Figure A may be represented as a string of b's and r's, where b represents a blue bead and r represents a red one, as follows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .

Now "B" wants to break the necklace at some point, lay it out straight, and then collect beads of the same color from one end until he reaches a bead of a different color, and do the same for the other end (which might not be of the same color as the beads collected before this).

Determine the point where the band should be broken so that the most number of beads can be collected!

When collecting beads, a white bead that is encountered may be treated as either red or blue and then painted with the desired color.

Constraints :

0<=N<=500

Input Format :

First line contains N, the number of beads. The next line contains a string of length N denoting the types of beads.

Output Format :

A single line containing the maximum of number of beads that can be collected from the supplied band.

Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?