USACO 1.1.4 – Broken Necklace


int main_beads() {
	int r, N, color(0), cur(0), w(0), pre(0), best(0);
	string s;
	fin >> N >> s;
	s = s + s;
	for (r = 0; r < 2 * N && pre + cur < N; r++) {
		if (s[r] != 'w' && s[r] != color)
			color = s[r], best = max(best, pre + cur), pre = cur - w, cur = w;
		w = (s[r] == 'w') ? w + 1 : 0, cur++;
	}

	fout << max(best, pre + cur) << endl;
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: