传送门
http://www.lydsy.com/JudgeOnline/problem.php?id=4236
题目大意
给定一个只由 J,O,I 三个字符组成的字符串,询问满足三种字符数相同的最长字串长度
题解
暴力是求前缀和然后
O(N2)
查询
我们求完前缀和后对
J,O,I
三个前缀和之间差分以下
这样我们发现要求的其实就是
J,O,I
相对大小不变
所以我们把它们排个序取个最大值即可
const
maxn=200005;
var
x,y:array[0..maxn,1..3]of longint;
i,j,k:longint;
n,ans,a:longint;
cha:char;
function max(a,b:longint):longint;
begin if a>b then exit(a) else exit(b); end;
procedure sort(l,r:longint);
var i,j,a,b,c,d,e:longint;
begin
i:=l; j:=r; a:=x[(l+r)div 2,1]; b:=x[(l+r)div 2,2]; c:=x[(l+r)div 2,3];
repeat
while (x[i,1]<a)or((x[i,1]=a)and(x[i,2]<b))or((x[i,1]=a)and(x[i,2]=b)and(x[i,3]<c)) do inc(i);
while (x[j,1]>a)or((x[j,1]=a)and(x[j,2]>b))or((x[j,1]=a)and(x[j,2]=b)and(x[j,3]>c)) do dec(j);
if not(i>j) then
begin
e:=1; d:=x[i,e]; x[i,e]:=x[j,e]; x[j,e]:=d;
e:=2; d:=x[i,e]; x[i,e]:=x[j,e]; x[j,e]:=d;
e:=3; d:=x[i,e]; x[i,e]:=x[j,e]; x[j,e]:=d;
inc(i); dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
begin
readln(n);
for i:=1 to n do
begin
read(cha);
y[i,1]:=y[i-1,1]; y[i,2]:=y[i-1,2]; y[i,3]:=y[i-1,3];
case cha of
'J':inc(y[i,1]);
'O':inc(y[i,2]);
'I':inc(y[i,3]);
end;
end;
readln;
for i:=1 to n do
begin
x[i,1]:=y[i,2]-y[i,1]; x[i,2]:=y[i,3]-y[i,2]; x[i,3]:=i;
end;
x[n+1,1]:=0; x[n+1,2]:=0; x[n+1,3]:=0;
sort(1,n+1);
ans:=0; a:=1; x[n+2,1]:=maxlongint; x[n+2,2]:=maxlongint; x[n+2,3]:=maxlongint;
for i:=2 to n+2 do
begin
if (x[i,1]<>x[a,1])or(x[i,2]<>x[a,2])
then begin ans:=max(ans,x[i-1,3]-x[a,3]); a:=i; end;
end;
writeln(ans);
end.