Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This is an algoritm problem from Topcoder SRM 566 Div2.

The problem can viewed here.

For those not having topcoder account the problem is described below:

Penguin Pals is a match making service that matches penguins to new friends, using the following procedure:

  1. Each penguin is asked a single question: "Do you prefer the color blue, or the color red?"
  2. All penguins are arranged so that they stand on a circle, equally spaced.
  3. The organizers draw some straight lines, connecting some pairs of penguins. Each penguin may only be connected to at most one other penguin. Two penguins cannot be connected if they prefer a different color.
  4. Each penguin who is connected to some other penguin follows the line to find their match.

The only problem with the above system was that it allowed penguins to collide if two lines crossed each other. Therefore, a new additional rule was adopted: no two lines may cross. Penguin Pals now has some penguins arranged on a circle (after step 2 of the above procedure). They need to know the maximum number of pairs of penguins they can create.

You are given a String colors whose i-th character represents the prefered color of the i-th penguin (0-based index) in the circular arrangement. The i-th character is 'R' if the i-th penguin prefers red and 'B' if the i-th penguin prefers blue. Return the maximum number of matched pairs that can be formed.

Example:

"RRBRBRBB"

Returns: 3

"BBBBB"

Returns: 2

"RRRBRBRBRBRB"

Returns: 5

My Approach:

Call the string s of length n. (Note that 0th and n-1th index are consecutive).

I used a recursive function recurse(string s,int i,int j) which is as follows:

int recurse(string s,int i,int j)
{
     if(i>=j)
           return 0;
     if(s[i]==s[j])
        return(1+recurse(s,i+1,j-1));
     else return max(recurse(s,i,j-1),recurse(s,i+1,j));
}

I start from i=0 and j=n-1, as they both will be consecutive if they are equal, call the the function with (i+1,j-1) and if not take both the possibilities and call the function recurse(s,i,j-1) and recurse(s,i+1,j) and will take the maximum of these two.

I called this function for every possible starting pair i.e.

for input "RRRBRRBB".

I called the function recurse() with inputs:

  1. s="RRRBRRBB" i=0 j=n-1
  2. s="RRBRRBBR" i=0 j=n-1 (Moved the string left and the earlier leftmost is now the rightmost)
  3. s="RBRRBBRR" i=0 j=n-1 (the same operation)

and so on until all the cases are covered.

But i got WA, and couldn't identify the flaw in my approach why it couldn't work.

share|improve this question
    
You solution failed into following test case RRBB – Толя Jan 15 '13 at 12:11
    
@Толя How it fails, my solution return 2 and i think the answer is also 2. Moreover I am not doubting on System judge I know my solution is not correct but couldn't identify a logical flaw. That's what i am here for. – Akashdeep Saluja Jan 15 '13 at 12:14
    
You solution returns 1. After first branching, solution search answer for RRB and RBB for both answer 1 and max too 1, as result 1. – Толя Jan 15 '13 at 12:20
    
@Толя I think you didn't understand my solution completely, i call the recurse function for "RRBB" , then for "RBBR", "BBRR" and "BRRB" and take the maximum of all those values. So for input "RBBR" my solution outputs 2. – Akashdeep Saluja Jan 15 '13 at 12:22
    
your provided code returns 1 and doing another than you describe. – Толя Jan 15 '13 at 12:27
up vote 3 down vote accepted

To correct you solution you should do following into each recursively call:

s="RRRBRRBB" i=0 j=n-1
s="RRBRRBBR" i=0 j=n-1 (Moved the string left and the earlier leftmost is now the rightmost)
s="RBRRBBRR" i=0 j=n-1 (the same operation)
and so on until all the cases are covered.

But I feels TLE on this case.

Solution: This is simple problem.

1) Remove all pairs from string where s[i] == s[(i+1) % n], and calculate count. (i from 0 to n-1).

2) Iterate #1 till your string not converted to "RBRBRBRB...RB" or "BRBRBRBRBR...BR", for this special casing result (length / 2) - 1;

share|improve this answer

It's probably worthwhile mentioning that the expected solutions are documented on the Problem Set Analysis page for SRM566.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.