Solution to a problem P1203 [[USACO1.1] broken necklace Broken Necklace]

Look upstairs, downstairs problem solution are greatly short, I released a long point! . . .      
This question is actually a simulation subject, with the most simple way is to string s becomes s + s + s, to be connected end to end to facilitate this way, then determines the current click s [i] and s [J] is equal or s [j] = 'w' time may continue, there is a need to pay attention that: being a white beads may be encountered as a red may also be used as blue. The emphasis here is the experience that is the current face full of w, and now all of a sudden there was a 'b', 'r' is not enough. When this can be said to be the i-th character 'w' down time without analog (except for the data - all as 'w').         
============================ perfect dividing line =================== ==========

#include <the iostream> 
#include <CString>
 the using  namespace STD;
 int main () 
{ 
    char S [ 700 ], C = 0 ;
     int n-, I, TEMP = 0 , NUM = 0 , W = 0 , ANS = 0 ; 
    CIN >> >> n- S; 
    the memcpy (S + n-, S, n-); // Copy 
    for (I = 0 ; I <* n- 2 ; I ++ )
         IF (S [I] == ' W ' ) NUM ++, ++ W; // if w, it as the current color, and counted the following helpful
        the else  IF (S [I] == C) NUM ++, W = 0 ; // current continuous color, count, W cleared 
        the else  // not, then 
        {
             IF (TEMP + NUM> ANS) ANS = NUM + TEMP; // do a comparison, the recording ANS 
            temp = NUM-W; W = NUM + . 1 ; W = 0 ; C = S [I]; // continuous color change, recording starts with temp, such wwwbrwrbww, generating wwwbrwrbwwwwwbrwrbww;
                                            / /   started so w is w ++; change temp = 0 in [3] when S; because no other colors. w seen on B; B =. 4;
                                            // time to change the color, temp = 4, num = 1 ; since no time continuous color B w w r have; and so on;
                                            //Repeated this way, in fact, the filter w, only r, b requiring only a single point on the left rrrbbbb continuous way on line changing point
                                            // disconnect it; of course be repeated, such as wwwwbbbbrrrr, which outputs a n-
                                            // the final statement is the determination 
        }
     IF (TEMP + NUM> ANS) ANS = NUM + TEMP; //
     COUT << (ANS> n-n-:? ANS) << endl; //
     return  0 ; 
}

 



Guess you like

Origin www.cnblogs.com/Sworddust/p/11427829.html