You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white, arranged at random. Here are two examples for n=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 .

Suppose you are to break the necklace at some point, lay it out straight, and then collect beads of the same color from one end until you reach 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 necklace should be broken so that the most number of beads can be collected.
Example

For example, for the necklace in Figure A, 8 beads can be collected, with the breaking point either between bead 9 and bead 10 or else between bead 24 and bead 25.

In some necklaces, white beads had been included as shown in Figure B above. When collecting beads, a white bead that is encountered may be treated as either red or blue and then painted with the desired color. The string that represents this configuration will include the three symbols r, b and w.

Write a program to determine the largest number of beads that can be collected from a supplied necklace.
PROGRAM NAME: beads
INPUT FORMAT
Line 1: N, the number of beads
Line 2: a string of N characters, each of which is r, b, or w
SAMPLE INPUT (file beads.in)

29
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

OUTPUT FORMAT
A single line containing the maximum of number of beads that can be collected from the supplied necklace.
SAMPLE OUTPUT (file beads.out)

11

OUTPUT EXPLANATION
Consider two copies of the beads (kind of like being able to runaround the ends). The string of 11 is marked.

wwwbbrwrbrbrrbrbrwrwwrbwrwrrb wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
****** *****

What kind of string does it wants me to create and how can i tell it that if there is one red between blues that it still goes on and count?

thanks
di Mo

Recommended Answers

All 7 Replies

You lose all formatting when you copy and paste here. I don't know what Figure A and Figure B are. Are they images? Was there spacing in this post? You can use code tags to preserve Courier type spacing (all characters equal width) - Otherwise the spacing gets stripped out:

[code]

// paste text where formatting matters here

[/code]

And you can always use "Preview Post" to see the post and make any corrections. But something clearly got lost in the posting here.

commented: Or you can just click 'reply with quote' on their post. +19

I believe from what we have here the string will be in the file "beads.in". the first line in the file will be the number of beads and the second line will be the string with the r,b,w characters. what it wants you to do is to parse through the string and find the point where when broken in half you will have the most amount of same colored beads in a row at both ends. then output that answer into a file named "beads.out". post some code of what you got if your still having a problem and i will be happy to help if you need some.

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

Or as iamthwee correctly pointed out, I could have just hit "reply with quote" and fixed the tags, so here it is.

so first to get the second line into my array how do i do that...
do i put it first in an helper array and put it then into my real array like that?

for(int i=0;i<N;i++)
		chain[i]=helper[N];

or do i set it right into the array like that

fin>>chain[500];

well the first line in the file will tell you how many beds are in the string. if you are allowed to use std::string then i would just do this

int numberOfBeads;
std::string neckless;
ifstream fin("beads.in");
fin >> numberOfBeads;
fin >> neckless;

if you can't use std::string in this assignment then just use a regular char array. hope this helps.

no i cant use a std::string or maybe i can but i dont know how it works so i would rather not use it. And yes that was what i thought. A char array the question i have how do i set every single bead to the single number in the array.
example when my bead is like that:
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

that i set 0=w 1=w 2=w 3=b 4=b 5=r ...

and jsut for the info i only know strcmp and strcpy...

The "number" is the index of the array element. So if you were to break between the beads at element 5 and 6 in the array, that would be the same as breaking between the 6th and 7th beads in the necklass if the beads are numbered 1 through n.

To read the entire line (assuming the only whitespace char on the line is the newline char) into a char array acting as a string use the >> with syntax like: inputStreamName >> stringName. No numbers needed. No strcpy or strcmp needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.