Files
contests/usaco/ch1/beads.cpp

116 lines
4.4 KiB
C++

/*
ID: philipp17
PROG: beads
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#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");
int n;
string s;
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;
}