#P1203. [IOI 1993 / USACO1.1] 坏掉的项链 Broken Necklace

[IOI 1993 / USACO1.1] 坏掉的项链 Broken Necklace

Description

You have a necklace made of nn beads that are red, white, or blue, arranged arbitrarily. Here are two examples for n=29n=29:

The first and second beads are marked in the picture.

The necklace in Figure A can be represented by the string: brbrrrbbbrrrrrbrrbbrbbbbrrrrb.

Suppose you break the necklace at some point, lay it out in a line, then from one end start collecting beads of a single color until you encounter a bead of a different color, and do the same from the other end (the color you collect on the two sides may differ). Determine where to break the necklace to collect the maximum number of beads.

For example, for the necklace in Figure A, breaking between bead 99 and bead 1010, or between bead 2424 and bead 2525, allows you to collect 88 beads.

A string representing a necklace that may include white beads will use exactly the three symbols r, b, w.

Write a program to determine the maximum number of beads that can be collected from the given necklace.

::::info[白色珠子是什么?] In some necklaces, there are also white beads (as in Figure B).

When collecting beads, any white bead you encounter may be treated as red or as blue. ::::

Input Format

The first line contains a positive integer nn, the number of beads.

The second line contains a string of length nn, where each character is r, b, or w.

Output Format

Output a single integer on one line: the maximum number of beads that can be collected from the given necklace.

29 
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

11

Hint

Constraints

For 100%100\% of the testdata, 3n3503\le n \le 350.

Translated from NOCOW.

USACO Training Section 1.1.

Translated by ChatGPT 5