Code: |
CONST fi = '';
VAR a : array[1..3000]of char;
ap : array['A'..'z']of boolean;
b : array[1..3000000]of char;
queue: array[1..3000000]of char;
dem, top, bot : longint;
s, w : qword;
n, m : longint;
f : text;
Procedure enter;
Var i: integer;
Begin
assign(f, fi);
reset(f);
readln(f, n, m);
for i:=1 to n do
begin
read(f, a[i]);
ap[a[i]]:= true;
w:= w+ord(a[i]);
end;
for i:=1 to m do read(f, b[i]);
close(f);
End;
Procedure solve;
Var i, j, top, bot : longint;
Begin
top:= 1; bot:= top; dem:= 0; s:= 0;
for i:= 1 to m do
begin
if ap[b[i]] then
begin
inc(top);
queue[top]:= b[i];
s:= s+ord(b[i]);
if (top-bot=n)and(s=w)then inc(dem);
if (top-bot>n) then
begin
s:=s-ord(queue[i-n]);
if w=s then inc(dem);
inc(bot);
end;
end
else if top>1 then
begin
s:= 0;
dec(top);
bot:= top;
end;
end;
End;
Procedure print;
Begin
writeln(dem);
End;
BEGIN
enter;
solve;
print;
END.
|
Mọi người coi sai chổ nào chỉ em với, em dùng queue kì lạ thế này ạ.
Em dùng mảng ap['A'..'z'] để đánh dấu các kí tự có trong dẫy kí tự a;
Với các kí tự có trong a ta đổi kí tự đó ra ord(a[i]) sao đó tổng cho s:= s+ord(a[i]);
Nếu không phải ta gắn cho s=0 lại;
Còn nếu w=s (với w=w+ord(a[i])(i:=1->n)) THEN inc(dem);
Cuối cùng write(dem);