Skip to content

Instantly share code, notes, and snippets.

@sarahhodne
Created March 10, 2012 11:34
Show Gist options
  • Save sarahhodne/2011198 to your computer and use it in GitHub Desktop.
Save sarahhodne/2011198 to your computer and use it in GitHub Desktop.
/*
ID: dvyjone1
PROG: beads
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
typedef struct bead {
struct bead *previous;
char color;
struct bead *next;
} Bead;
typedef struct bead *BeadPtr;
int count_beads(BeadPtr first_bead, char color, int max, int backwards) {
int i;
BeadPtr current_bead = first_bead;
for (i = 0; i < max; ++i) {
if (current_bead->color == color || current_bead->color == 'w') {
if (backwards) {
current_bead = current_bead->previous;
} else {
current_bead = current_bead->next;
}
} else {
break;
}
}
return i;
}
char find_color(BeadPtr bead, int backwards, int max) {
if (max == 0) return bead->color;
if (bead->color == 'r' || bead->color == 'b') return bead->color;
if (backwards) return find_color(bead->previous, backwards, --max);
else return find_color(bead->next, backwards, --max);
}
int main() {
std::ofstream fout ("beads.out");
std::ifstream fin ("beads.in");
int N, i;
fin >> N;
BeadPtr beads = (BeadPtr)calloc(N, sizeof(Bead));
for (i = 0; i < N; ++i) {
fin >> beads[i].color;
if (i == 0) {
beads[i].previous = beads + N-1;
beads[i].next = beads + 1;
} else if (i == N-1) {
beads[i].previous = beads + N - 2;
beads[i].next = beads;
} else {
beads[i].previous = beads + i - 1;
beads[i].next = beads + i + 1;
}
}
int max = 0;
for (i = 0; i < N; ++i) {
int j = 0, bead_count = 0;
BeadPtr current_bead = beads + i;
bead_count = count_beads(current_bead, find_color(current_bead, 0, N), N, 0);
bead_count += count_beads(current_bead->previous, find_color(current_bead->previous, 1, N), N, 1);
std::cout << i << "\t" << bead_count << "\t" << current_bead->color << "\t" << current_bead->previous->color << std::endl;
if (bead_count > max) max = bead_count;
}
if (max > N) max = N;
fout << max << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment