少女觉
题目大意
给出一个长度为N的只含
数据范围
题解
设每一段中字母A、
在证明一个结论,假若前i个已经被分了,现在有两个位置可选为分割点,分别为l,
那我们只需要顺着O(
Code(Pascal)
var
sz:array[0..120000,1..2] of longint;
t,n,m,j,k,i,o,ans,u,js:longint;
ff,kk:array[0..1] of int64;
ch,cc:char;
function gcd(a,b:int64):int64;
var
t:int64;
begin
repeat
t:=a mod b;
a:=b;
b:=t;
until t=0;
exit(a);
end;
begin
readln(t);
for k:=1 to t do
begin
readln(n);
ch:='0';
o:=0;
for i:=1 to n do
begin
read(m);
read(cc); readln(cc);
if cc<>ch then
begin
inc(o);
if cc='W' then
sz[o,1]:=0 else sz[o,1]:=1;
sz[o,2]:=m;
end
else sz[o,2]:=sz[o,2]+m;
ch:=cc;
end;
ff[0]:=0;
ff[1]:=0;
for i:=1 to o do
inc(ff[sz[i,1]],sz[i,2]);
if ff[0]*ff[1]=0 then writeln(ff[0]+ff[1])
else
begin
js:=gcd(ff[0],ff[1]);
ff[0]:=ff[0] div js;
ff[1]:=ff[1] div js;
kk[0]:=0;
kk[1]:=0;
ans:=0;
for i:=1 to o do
begin
u:=sz[i,1];
inc(kk[u],sz[i,2]);
if kk[1-u]<>0 then
if kk[1-u] mod ff[1-u]=0 then
if (kk[1-u] div ff[1-u])*ff[u]<=kk[u] then
if (kk[1-u] div ff[1-u])*ff[u]>=kk[u]-sz[i,2] then
begin
inc(ans);
kk[u]:=kk[u]-(kk[1-u] div ff[1-u])*ff[u];
kk[1-u]:=0;
end;
end;
writeln(ans);
end;
end;
end.