Pagini recente » Cod sursa (job #2055213) | Cod sursa (job #239839) | Cod sursa (job #2585545) | Cod sursa (job #108150) | Cod sursa (job #104587)
Cod sursa(job #104587)
var fi,fo:text;
sir:string;
cuv:array[1..50000]of string[21];
solutii:array[1..10000000]of string[21];
cont,i,ct1,ct,ct2,ct3,rez,rez1:longint;
procedure heapsort(n:longint);
var i,j,k:longint;
aux:string[21];
begin
for i:=1 to n do
begin
j:=i;
while (j div 2<>0) and (solutii[j div 2]<solutii[j]) do
begin
aux:=solutii[j div 2];
solutii[j div 2]:=solutii[j];
solutii[j]:=aux;
j:=j div 2;
end;
end;
i:=n;
while i>1 do
begin
aux:=solutii[1];
solutii[1]:=solutii[i];
solutii[i]:=aux;
dec(i);
j:=1;
while (j>0) do
begin
k:=2*j;
if (k>i) then break;
if (k+1<=i) and (solutii[k+1]>solutii[k]) then inc(k);
if solutii[j]>=solutii[k] then break;
aux:=solutii[j];
solutii[j]:=solutii[k];
solutii[k]:=aux;
j:=k;
end;
end;
end;
function findmax(x:longint; aux:string[21]):longint;
var st,dr,mij:longint;
begin
st:=1; dr:=x; findmax:=0;
while st<=dr do
begin
mij:=(st+dr) shr 1;
if (solutii[mij]=aux) then
begin
findmax:=mij;
st:=mij+1;
if solutii[mij+1]>aux then exit;
end
else
dr:=mij-1;
end;
end;
function findmin(x:longint; aux:string[21]):longint;
var st,dr,mij:longint;
begin
st:=1; dr:=x; findmin:=0;
while st<=dr do
begin
mij:=(st+dr) shr 1;
if (solutii[mij]=aux) then
begin
findmin:=mij;
dr:=mij-1;
if solutii[mij-1]<aux then exit;
end
else
st:=mij+1;
end;
end;
begin
assign(fi,'abc2.in'); reset(fi);
assign(fo,'abc2.out'); rewrite(fo);
readln(fi,sir);
while not eof(fi) do
begin
inc(ct);
readln(fi,cuv[ct]);
end;
ct1:=0; ct2:=length(cuv[1]); ct3:=length(sir); cont:=0;
while ct1<ct3-3 do
begin
inc(ct1);
solutii[ct1]:=copy(sir,1,ct2);
delete(sir,1,1);
end;
heapsort(ct1);
for i:=1 to ct do
begin
rez1:=findmax(ct1,cuv[i]);
if rez1<>0 then
begin
rez:=rez1-findmin(ct1,cuv[i])+1;
cont:=cont+rez;
end;
end;
writeln(fo,cont);
close(fi);
close(fo);
end.