First, facilitate the necklace in sequence, and calculate the longest distance each bead can extend to the left.
At the convenience of the end, compare the type of beads at the beginning and end of the necklace. If they are the same, update the front part of the necklace.
Then, the distance from the right direction of each bead is calculated based on a reverse link.
Last part of the necklace updated
Errors made during the process:
(1) I did not consider the case where a whole necklace is a bead.
(2) For example, In bwrwrr, the W in front of the first r should also be included in the left chain of R; but if the W is disconnected from the back, KW should be in a chain
So I add a secondary array Wn (indicating the number of W before position I), if s [I-1] is W, if s [I] is not w, Wn is used to calculate the left chain;
The calculation of the right chain is the same.
It seems that many auxiliary arrays are used for calculation.
# Include <fstream>
# Include <iostream>
# Include <string>
Using namespace STD;
Int main ()
{
Ifstream fin ("beads. In ");
Ofstream fout ("beads. Out ");
Int N; // Number of beads
String s; // necklace
Fin> N> S;
Int right [350] = {0 };
Int lef [350] = {0 };
Int wnl [350] = {0}; // left number, W count
Int WNR [350] = {0}; // right number, W count
Int BP [350] = {0}; // split point
Char C = 0;
Int I; // Loop
// Find the largest string on the left
For (I = 0; I <n; I ++)
{
// Count the number of consecutive W
If (I> 0 & S [I] = 'W' & S [I-1] = 'W ')
{
Wnl [I] = wnl [I-1] + 1;
}
// Calculate the number of consecutive identical beads
If (s [I] = c | s [I] = 'W ')
{
Lef [I] = lef [I-1] + 1;
}
Else
{
If (s [I-1] = 'W ')
{
Lef [I] = wnl [I-1] + 1;
}
}
If ('w '! = S [I])
{
C = s [I];
}
// Cout <wnl [I] <"";
}
// Update the first few beads based on the last beads.
If (s [0] = c | s [0] = 'W ')
{
Lef [0] = lef [n-1] + 1;
// Cout <Endl <lef [0];
Int temp = 1;
While (temp <= N-1) & (s [temp] = c | s [temp] = 'W '))
{
Lef [temp] = lef [temp-1] + 1;
Temp ++;
}
}
// Find the largest string on the right
C = 0;
For (I = n-1; I> = 0; I --)
{
// Count the number of consecutive W
If (I <n-1 & S [I] = 'W' & S [I + 1] = 'W ')
{
WNR [I] = WNR [I + 1] + 1;
}
If (s [I] = c | s [I] = 'W ')
{
Right [I] = right [I + 1] + 1;
}
Else
{
If (s [I + 1] = 'W ')
{
Right [I] = WNR [I + 1] + 1;
}
}
If ('w '! = S [I])
{
C = s [I];
}
}
// Based on the first bead, refine the last few beads
If (s [n-1] = c | s [n-1] = 'W ')
{
Right [n-1] = right [0] + 1;
// Cout <right [n-1];
Int temp = n-2;
While (temp> = 0 & (s [temp] = c | s [temp] = 'W '))
{
Right [temp] = right [temp + 1] + 1;
Temp --;
}
}
// Cut at the maximum vertex position, number of beads
Int P =-1;
For (I = 0; I <n; I ++)
{
If (I = N-1)
{
BP [I] = lef [I] + right [0] + 2;
}
Else
{
BP [I] = lef [I] + right [I + 1] + 2;
}
If (BP [I]> P)
{
P = BP [I];
}
// Cout <lef [I] <"" <right [I] <Endl;
}
// If it is solid beads
If (P> N)
P = N;
Fout <p <Endl;
// Cout <Endl <p <Endl;
Return 0;
}