Question
A chain contains N red, white, or blue beads (3 b b b r b r b b r r 12 rbbr b r rbr
- 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.
b b b r b r b b r r 12 rbbr b r rbr Figure 1 r r r DDD b b b r W r b r W b b b 1 2 brrb r rrw Figure 2 b b r W LDD b b r r
Step by Step Solution
3.54 Rating (151 Votes )
There are 3 Steps involved in it
Step: 1
ANSWER Heres a Python program that reads input from a file named inputtxt and writes the output to a ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started