Finished beads and milk2 for usaco
This commit is contained in:
@@ -7,9 +7,12 @@ LANG: C++
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
enum Colour{ red, blue, white };
|
||||
|
||||
int main() {
|
||||
ofstream fout ("beads.out");
|
||||
ifstream fin ("beads.in");
|
||||
@@ -19,7 +22,95 @@ int main() {
|
||||
|
||||
fin >> n >> s;
|
||||
|
||||
|
||||
string doubleNecklace = s.append(s);
|
||||
|
||||
Colour lastBead = white, prewhiteBead = white;
|
||||
int currentLength = 0, redLength = 0, blueLength = 0, longestChain = 0, preWhiteLength = 0, inbetweenWhiteLength = 0;
|
||||
|
||||
// The most intricate scenario is the following scenario:
|
||||
// white - red - white - blue - white
|
||||
// or equivalently:
|
||||
// white - blue - white - red - white
|
||||
|
||||
// When white beads are enclosed by beads of the same colour we regard the beads as those colour
|
||||
|
||||
|
||||
for(const char& bead : doubleNecklace){
|
||||
if(bead == 'w'){
|
||||
if(lastBead == white){
|
||||
currentLength++;
|
||||
} else if(lastBead == red){
|
||||
prewhiteBead = red;
|
||||
redLength = currentLength;
|
||||
currentLength = 1;
|
||||
} else{ // Last bead is blue
|
||||
prewhiteBead = blue;
|
||||
blueLength = currentLength;
|
||||
currentLength = 1;
|
||||
}
|
||||
longestChain = max(longestChain, currentLength + redLength + blueLength + prewhiteBead + inbetweenWhiteLength);
|
||||
lastBead = white;
|
||||
|
||||
} else if(bead == 'r'){
|
||||
if(lastBead == red){
|
||||
currentLength++;
|
||||
longestChain = max(longestChain, currentLength + preWhiteLength + blueLength + inbetweenWhiteLength);
|
||||
} else if(lastBead == blue){
|
||||
longestChain = max(longestChain, currentLength + redLength + preWhiteLength + inbetweenWhiteLength);
|
||||
blueLength = currentLength;
|
||||
redLength = 0;
|
||||
currentLength = 1;
|
||||
preWhiteLength = inbetweenWhiteLength;
|
||||
inbetweenWhiteLength = 0;
|
||||
} else{ // Last bead is white
|
||||
if(prewhiteBead == red){
|
||||
currentLength += redLength + 1;
|
||||
redLength = 0;
|
||||
longestChain = max(longestChain, currentLength + preWhiteLength + blueLength + inbetweenWhiteLength);
|
||||
} else{
|
||||
longestChain = max(longestChain, currentLength + blueLength + redLength + preWhiteLength + inbetweenWhiteLength);
|
||||
preWhiteLength = inbetweenWhiteLength;
|
||||
inbetweenWhiteLength = currentLength;
|
||||
redLength = 0;
|
||||
currentLength = 1;
|
||||
}
|
||||
}
|
||||
lastBead = red;
|
||||
|
||||
} else if(bead == 'b'){
|
||||
if(lastBead == blue){
|
||||
currentLength++;
|
||||
longestChain = max(longestChain, currentLength + redLength + preWhiteLength + inbetweenWhiteLength);
|
||||
} else if(lastBead == red){
|
||||
longestChain = max(longestChain, currentLength + blueLength + preWhiteLength + inbetweenWhiteLength);
|
||||
redLength = currentLength;
|
||||
blueLength = 0;
|
||||
currentLength = 1;
|
||||
preWhiteLength = inbetweenWhiteLength;
|
||||
inbetweenWhiteLength = 0;
|
||||
} else{ // Last bead is white
|
||||
if(prewhiteBead == blue){
|
||||
currentLength += blueLength + 1;
|
||||
blueLength = 0;
|
||||
longestChain = max(longestChain, currentLength + redLength + preWhiteLength + inbetweenWhiteLength);
|
||||
} else{
|
||||
longestChain = max(longestChain, currentLength + blueLength + redLength + preWhiteLength + inbetweenWhiteLength);
|
||||
preWhiteLength = inbetweenWhiteLength;
|
||||
inbetweenWhiteLength = currentLength;
|
||||
blueLength = 0;
|
||||
currentLength = 1;
|
||||
}
|
||||
}
|
||||
lastBead = blue;
|
||||
|
||||
} else{
|
||||
cout << "unexpected input" << endl;
|
||||
return 0;
|
||||
}
|
||||
// cout << bead << " CurrentLength: " << currentLength << " RedLength: " << redLength << " BlueLength: " << blueLength << " Prewhitelength: " << preWhiteLength << " Inbetweenwhite: " << inbetweenWhiteLength << " LongestChain: " << longestChain << endl;
|
||||
}
|
||||
|
||||
fout << min(longestChain, int(s.size()) / 2) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user