PBCWRI - Writing
Tác giả: ll931110
Ngôn ngữ: Pascal
{$MODE DELPHI} {$H+}
Program PBCWRI;
Const
input = '';
output = '';
Type
PNode = ^TNode;
TNode = record
val: char;
link: PNode;
end;
Var
n,m,count,res: integer;
a,b: string;
curr,num: array['A'..'z'] of integer;
Front,Rear: PNode;
Procedure init;
Var
f: text;
i: integer;
Begin
Assign(f, input);
Reset(f);
Readln(f, n, m);
Readln(f, a);
Readln(f, b);
Close(f);
Fillchar(num, sizeof(num), 0);
For i:= 1 to n do inc(num[a[i]]);
End;
Procedure push(v: char);
Var
P: PNode;
Begin
inc(count);
inc(curr[v]);
New(P);
P^.val:= v;
P^.link:= nil;
If Front = nil then Front:= P else Rear^.link:= P;
Rear:= P;
End;
Procedure pop;
Var
P: PNode;
v: char;
Begin
v:= Front^.val;
dec(count);
dec(curr[v]);
P:= Front^.Link;
Dispose(Front);
Front:= P;
End;
Procedure solve;
Var
i: integer;
Begin
Fillchar(curr, sizeof(curr), 0);
res:= 0;
count:= 0;
Front:= nil;
For i:= 1 to m do
Begin
push(b[i]);
If curr[b[i]] > num[b[i]] then
While curr[b[i]] > num[b[i]] do pop
else
if count = n then
Begin
inc(res);
pop;
End;
End;
End;
Procedure printresult;
Var
f: text;
Begin
Assign(f, output);
Rewrite(f);
Writeln(f, res);
Close(f);
End;
Begin
init;
solve;
printresult;
End.