Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DZone's Guide to

· ·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views
```// Here's my solution to the USACO Beads algorithm problem. In C++,
// using a circularly linked list

```
#include

#include

using namespace std;

char color;
bool breakflag;
bool firstbreak;
};

if (newNode == NULL) {
return NULL;
}
newNode->color = color;
newNode->breakflag = false;
newNode->firstbreak = false;
newNode->next = node->next;
newNode->previous = node;
node->next = newNode;
return newNode;
}

do {
if (goLeft) {
node = node->previous;
} else {
node = node->next;
}
if ((node->color != 'w') && (node->color != curcolor)) {
return node;
}
} while (node != current);
return NULL;
}

while ((current->color == curcolor) || (current->color == 'w')) {
if (goLeft) {
current = current->previous;
} else {
current = current->next;
}
if (node == current) return NULL;
}
current->next->breakflag = true;
return current->next;
}

int countFromBreak(bead* current, bool goLeft, char curcolor) {
int count = 0;
while ((current->color == curcolor) || current->color == 'w') {
if (goLeft) {
current = current->previous;
} else {
current = current->next;
}
++count;
}
return count;
}

int main() {

current = NULL;

first->next = NULL;
first->breakflag = false;
current = first;
for (int x = 1; x < numbeads; x++) {
}
current->next = first;
first->previous = current;
current = first;

char curcolor = current->color;

if (curcolor == 'w') {
current = getNextColor(current, true, 'w');
if (current == NULL) {
return 0;
} else curcolor = current->color;
}
current = searchForBreak(current, true, curcolor);
if (current == NULL) {
return 0;
}
current->firstbreak = true;
current->breakflag = true;

int maxsize = 0;
int newsize = 0;

do {
newsize = countFromBreak(current, false, curcolor);
curcolor = (curcolor == 'b') ? 'r' : 'b';
newsize += countFromBreak(current->previous, true, curcolor);
if (newsize > maxsize) maxsize = newsize;
current = searchForBreak(current->previous, true, curcolor);
} while (!current->firstbreak);

fout << maxsize << endl;

return 0;
}

``````
Topics:

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Opinions expressed by DZone contributors are their own.