A chain contains N red, white, or blue beads (3 b b b r b r b
Question:
- A chain contains N red, white, or blue beads (3<=N<=500), arranged at random. Figures 1 and 2 show examples for N=29 (r : red bead, b : blue bead, w : white bead).
Figure 1 Figure 2
The numbers 1 and 2 refer to the first and second beads, respectively. Figure 1 shows a chain with blue and red beads only and will be given in the data as follows:
brbrrrbbbrrrrrbrrbbrbbbbrrrrb
Suppose you break the chain at some point and lay it out in a straight line. You collect beads of the same color from one end until you reach a bead of a different colour. You repeat this from the other end, noting that the beads collected now could be of a different colour from those collected previously.
Some chains may contain white beads as shown in Figure 2. When collecting beads, a white bead encountered may be treated as either red or blue.
Write a program to determine the point where a given chain should be broken so that the most number of beads can be collected.
For example, for the chain in Figure 1, 8 beads can be collected, with the breaking point either between bead 9 and bead 10 or between bead 24 and bead 25.
Input: The first line contains N and the second line contains N beads (r, b or w). For example,
29
brbrrrbbbrrrrrbrrbbrbbbbrrrrb
Output: The maximum of number of beads that can be collected from the given chain. For the chain in Figure 1, you should output
8
Data must be read from the file input.txt and output sent to the file output.txt.
Foundations in Microbiology
ISBN: 978-0073375298
8th edition
Authors: Kathleen Park Talaro, Barry Chess