【USACO题库】1.1.4 Broken Necklace破碎的项链

这一题淼,只是因为项链是一个圆,所以在头和尾的地方要注意一下!

    var
        a:array[1..35001] of longint;
        b:array[1..35001] of char;
        n,i,k,w,max:longint;
        x:char;
	begin
        //assign(input,'necklace.in');reset(input);
        //assign(output,'necklace.out');rewrite(output);
        readln(n);
        k:=1;
        read(b[1]);
        a[1]:=1;
        for i:=2 to n do
        begin
                read(b[i]);
                if (b[i]=b[i-1])or(b[i]='w')or(b[i-1]='w') then inc(a[k])
                else
                begin
                        inc(k);
                        w:=i-1;
                        a[k]:=0;
                        while b[w]='w' do
                        begin
                                dec(w);
                                inc(a[k]);
                        end;
                end;
        end;
        if k=1 then
        begin
                writeln(a[1]);
                exit;
        end;
        if ((b[n]=b[1])or(b[n]='w')or(b[1]='w'))and(n<>1) then
        begin
                a[1]:=a[1]+a[k];
                a[k]:=a[1];
        end;
        for i:=1 to k-1 do
                if a[i]+a[i+1]+1>max then max:=a[i]+a[i+1]+1;
        if max>n then writeln(n)
        else writeln(max);
        //close(input);
        //close(output);
	end.

你可能感兴趣的:(USACO第一部分)