首页 > 代码库 > Broken Necklace
Broken Necklace
题解:
模拟。
枚举每一个地方断开,之后从断开点往前、往后寻找连续子序列就行了,不过要注意w的处理,还有答案不可能超过n。
{
ID:h1956701
LANG:PASCAL
PROB:beads
}
var l,i,x,ans:longint;
s,s1:ansistring;
function h(k:longint):longint;
begin
if k=0 then k:=l;
exit(k);
end;
function pd(k,p:longint):longint;
var ans,x:longint;
begin
ans:=0;
while s[k]=‘w‘ do
begin
inc(ans);
if ans>=l then exit(l);
k:=h((k+p)mod l);
end;
x:=k;
while (s[k]=s[x])or(s[k]=‘w‘) do
begin
inc(ans);
if ans>=l then exit(l);
k:=h((k+p)mod l);
end;
exit(ans);
end;
begin
assign(input,‘beads.in‘);
reset(input);
assign(output,‘beads.out‘);
rewrite(output);
readln(l);
readln(s1);
for i:=1 to l do
begin
s:=s1;
x:=pd(h(i-1),-1)+pd(i,1);
if x>ans then ans:=x;
end;
if ans>l then ans:=l;
writeln(ans);
close(input);
close(output);
end.
Broken Necklace