还是模拟,不过这也算是usaco中第一道不水的题,因为是2年前做的,觉得还挺难,现在也觉得好水了。。。

program beads;
var
  c:char;
  a:array[1..700] of char;
  max,s,j,k,i,left,right,n:integer;
begin
  assign(input,'beads.in');
  reset(input);
  assign(output,'beads.out');
  rewrite(output);
  readln(n);
  for i:=1 to n do
    read(a[i]);
  left:=1;
  right:=n;
  for k:=0 to n-1 do
  begin
    s:=0;
    c:=a[left];
    inc(s);
    i:=left;
    if c<>'w' then
    begin
      while (a[i+1]=c) or (a[i+1]='w') do inc(i);
      s:=s+(i-left);
    end
    else
    begin
      while a[i+1]=c do inc(i);
      c:=a[i+1];
      inc(i);
      while (a[i+1]=c) or (a[i+1]='w') do inc(i);
      s:=s+(i-left);
    end;
    c:=a[right];
    inc(s);
    j:=right;
    if c<>'w' then
    begin
      while (a[j-1]=c) or (a[j-1]='w') do dec(j);
      s:=s+(right-j);
    end;
    if s>n then
    begin
      writeln(n);
      close(input);
      close(output);
      halt;
    end;
    if s>max then max:=s;
    inc(right);
    a[right]:=a[left];
    inc(left);
  end;
  writeln(max);
  close(input);
  close(output);
end.